#DEFINE label substitution
#DEFINE label(param1,param2...) substitution
The #define directive can be used in two ways -
2) To define a single line substitution for use within the program.
Do not confuse the use of '#define' and 'symbol ='. '#define' is a directive and, when used with #ifdef, determines which sections of code are going to be compiled. ‘symbol = ’ is a command used within actual programs to re-label variables and pins.
When using a #define to specify a substitution take care to not inadvertently include an "=" before the substitution if it is not required as it will be taken as part of the substitution; this may cause unexpected syntax errors when the substition is used, for example -
#define SIZE = 20 ; Most likely incorrect
let w0 = SIZE
When expanded this will substitute as "let w0 = = 20" which is incorrect and will generate a syntax error. The correct substitution should be without the "=" symbol -
#define SIZE 20 ; Most likely correct. No "=" symbol
let w0 = SIZE
The #define substitution can be any arbitrary text; a number, variable name, command or more complicated substitution.
The substition is the entire text to the end of the line so inluding a comment within that line can cause issues when the subtitution is later made. The above example works but this will give a syntax error -
#define SIZE 20 ; Size of data table
sertxd( "Size=", #SIZE, cr, lf )
The substitition would produce -
sertxd( "Size=", #20 ; Size of data table, cr, lf )
The substituted semi-colon acting as a premature end of the 'sertxd' command, causing a syntax error.
A substitution can have parameters which allow values to be passed into the substitution. For example the following will create an expansion which can be used to report the progress of executing code which may be useful in debugging -
#define AT(where) sertxd( "now executing - ", where, cr, lf )
AT( "main" )
b0 = b0 + 1
AT( "mysub" )
Note that #define only allows a substitution to be defined on a single line. To use multi-line substitutions use the #macro command
An example of defining and undefining a symbol.
This substitutes the text "millisonds" (including quotation characters) wherever the ms appears in the source code
Submit Your Own Code!
You must be logged in to submit code examples. Login now.