ZBasic Language Reference
75
ZBasic Microcontrollers
#define <identifier> [ [=] <expression> ]
Here, <identifier> is the name of the conditional identifier that you want to define. You may also
give it a value, represented by <expression>, which may be an integral or string type. If you do not
specify a value, a default value of 1 is used. The expression may include literals or identifiers previously
defined using #define and some built-in constants (see Section 3.8).
Examples
#define EXPERIMENTAL
#define Version 23
#define Greeting = "Hello"
If you attempt to define a conditional identifier that is already defined, you will get an error message to
that effect. If you want to redefine a conditional identifier you must first undefine the existing one using
the directive:
#undef <identifier>
If the specified identifier is not actually defined, no error message will be issued so you may freely use
this directive to ensure that no definition exists prior to defining a conditional identifier. Note that
undefining an identifier that was defined on the command line only has effect in the current module. All
other modules will see the original value.
Once you have defined your conditional identifiers, you may use them in conditional directives that are
similar to If statements. The first two forms presented below are complementary.
#ifdef <identifier>
<other-text>
#endif
#ifndef <identifier>
<other-text>
#endif
The first form specifies that if the given <identifier> is defined, the compiler should process the text
up to the matching #endif but if the <identifier> is not defined, the compiler should ignore the text
up to the matching #endif. The second form has the opposite effect.
Example
#ifdef EXPERIMENTAL
Call TestSetup(i)
#endif
This allows the subroutine call to be compiled into the application if EXPERIMENTAL is defined, otherwise
it is left out.
As you might have already guessed, the conditional syntax also allows an #else clause.
#ifdef EXPERIMENTAL
ver = "X006"
#else
ver = "V1.4"
#endif
An alternate form of conditional directive allows you to specify an expression involving conditional
identifiers and integer or string literals, the Boolean value of which determines whether the code within
the conditional block is processed normally or not.
|