- Name:
- #define
- Syntax:
-
#DEFINE label
#DEFINE label substitution
#DEFINE label(param1,param2...) substitution
- Description:
-
The #define directive can be used in two ways -
1) To define a label to use in a #ifdef or #ifndef statements.
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 = SIZEWhen 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 = SIZEThe #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 )
main:
AT( "main" )
b0 = b0 + 1
gosub mysub
goto main
mysub:
AT( "mysub" )
returnNote that #define only allows a substitution to be defined on a single line. To use multi-line substitutions use the #macro command
Using #define
An example of defining and undefining a symbol.
- Code Example:
-
#define clock8 #ifdef clock8 let b1 = 8 #else let b1 = 4 #endif #undefine clock8 #ifndef clock8 let b2 = 4 #else let b2 = 8 #endif
Copy Code Submit an Example
substitution
This substitutes the text "millisonds" (including quotation characters) wherever the ms appears in the source code
- Code Example:
-
#define ms "milliseconds" main: sertxd( #w0, ms, cr, lf ) w0 = w0 + 1 pause 1000 goto main
Copy Code Submit an Example
Command expansion
A simple define to display a number on line one of an LCD
- Code Example:
-
symbol lcdpin = B.7 #define lcdout( number ) serout lcdpin, n2400, ( 254, 128, #number, " " ) main: lcdout( w0 ) w0 = w0 + 1 pause 1000 goto main
Copy Code Submit an Example
Submit Your Own Code!
You must be logged in to submit code examples. Login now.