#DEFINE

Syntax:

    #DEFINE NAME [String]

    or

    #DEFINE NAME [ = String]

Explanation:

#DEFINE allows you to declare string-based preprocessor constants.

This directive defines a text substitution string. Wherever name is encountered in the program or assembly code, string will be substituted.

The expansion is done recursively, until there is nothing more to expand and the compiler can continue analysing the resulting code.

The use of an existing constant is supported. The order of constants when using an existing constant is strict. The constant must exist prior to use.

To ensure evaluation of a calculation, a = must be used. This is strict.

#UNDEFINE can be used to make the compiler delete an existing constant.

Using the directive with no string causes a definition of name to be noted internally, and it may be tested for using the #ifdef directive/conditional processing. See the examples below.

Constants defined with this method are available for viewing in the CDF file. Creation of the CDF file is controlled with the Preferences Editor.

GCBASIC does not support creating a SUBroutine or FUNCTION with this directive.

Examples:

This program shows the creation of constants, the processing and showing of constants within a script, creation of specific constants within a script, and use of constants within a program.

    #chip MEGA4809
    #option Explicit


   // Numeric constants
        #define LENGTH 20
        #define CONTROL 0x19,7
        #define SINGLEPI = 22/7                                         // evaluated numeric string
        #define INTPI = INT(22/7)                                       // evaluated numeric string
        #define FACTOREDPI = INT((22/7 - INT(22/7))*1000)               // evaluated numeric string
        #define LENGTHSQUARED = LENGTH * LENGTH                         // evaluated numeric string
        #define PI 3.142
        #define RADIUS 10
        #define CIRCUMFERENCECALC = PI * RADIUS                         // evaluated numeric string, with constant substitution
        #define FACTORISEDCIRCUMFERENCE = INT(CIRCUMFERENCECALC*100)    // evaluated numeric string, with constant substitution


    //String(s) require double quotes and NO `=` assignment
        #define MYSTRING "This is a string"
    //  A string assignment is not required
        #define ACONSTANTTHATEXISTS

    // Macros are not supported.. just define the sub!
        // #define BADPOSITION(XX,YY,ZZ) (YY-(2 * ZZ +XX))

    // Unused constants that are invalid may not report an error until you try to use them within your program - as in this example
        // String assignment with an equal sign will fail. Do not use `=`
        #define BADMYSTRING = "This is a string"


    #script
        //Scripts can modify and manage CONSTANTS

        // Warning can be used to show values during compilation
        WARNING LENGTH
        WARNING CONTROL
        WARNING SINGLEPI
        WARNING INTPI
        WARNING FACTOREDPI
        WARNING MYSTRING

        If DEF(ACONSTANTTHATEXISTS) Then
            WARNING "The constant ACONSTANTTHATEXISTS exists"
        End If

        // Test for a value in the [ChipData] section of the DAT file. Always has the prefix CHIP
        If DEF(CHIPAVRDX) Then
            WARNING "This is an AVRDX chip"
        End If

        // Good practice constant testing, see the code below for BAD and GOOD practice

            // Set the constant to 0 so we can use this to test for validity
            // Use the prefix SCRIPT as this is clear in the program
            SCRIPTAN1CONSTANT = 0
            If DEF(AVRDX) Then
                // Is this an AVRDx chip
                If DEF(AIN1) Then
                    SCRIPTAN1CONSTANT =  AIN1
                End If
            End If
            If NODEF(AVRDX) Then
                // This is NOT an AVRDx chip
                If DEF(AVR) Then
                    // This is an AVR
                    SCRIPTAN1CONSTANT =  AN1
                End If
                If DEF(PIC) Then
                    // This is a PIC
                    SCRIPTAN1CONSTANT =  ANA1
                End If
            End If
            // Now validate the result
            If SCRIPTAN1CONSTANT = 0 Then
                WARNING Script has determined that no valid ADC port exists, or some other message
            End If

    #endscript

    // Some conditional examples

    #IF DEF( CONSTANTTHATEXISTS )
        //! Cause a compiler error - as the constant exists. Remove comment to test
    #ELSE
        //! Cause a compiler error - as the constant does not exist. Remove comment to test
    #ENDIF

    #IFDEF Oneof(CHIP_18F24K40,CHIP_18F25K40,CHIP_18F26K40,CHIP_18F27K40,CHIP_18F45K40,CHIP_18F46K40,CHIP_18F47K40,CHIP_18F65K40,CHIP_18F66K40,CHIP_18LF24K40, CHIP_18LF25K40, CHIP_18LF26K40, CHIP_18LF27K40, CHIP_18LF45K40, CHIP_18LF46K40, CHIP_18LF47K40, CHIP_18F65K40, CHIP_18LF65K40, CHIP_18F66K40, CHIP_18LF66K40, CHIP_18F67K40, CHIP_18LF67K40 )
        //~ Do something
    #ENDIF

    dim myStringVar as String
    myStringVar = MYSTRING          ' <<< assigning a string-type #define to a String variable

    // BAD PRACTICE - code is hard to understand
        // Use constant testing to determine the correct ADC to read. Bad practice, see the #SCRIPT section
        dim mybyteVar as Byte
        #IF DEF(AIN1)
            mybyteVar = readAD( AIN1 )
        #ELSE
            #IF DEF(ANA1)
                mybyteVar = readAD( ANA1 )
            #ELSE
                mybyteVar = readAD( AN1 )
            #ENDIF
        #ENDIF

    // GOOD PRACTICE
        dim mybyteVar as Byte
        mybyteVar = readAD( SCRIPTAN1CONSTANT )

    dim myArray(2)
    myArray = CONTROL

Key line: myStringVar = MYSTRING — assigns the text substituted from the #define MYSTRING "This is a string" constant into a real runtime String variable; unlike the numeric #define`s above it, a string constant is used without an `= in its own definition, and here it is consumed exactly like any other string value.



See Also:

  • Constants — the general constants/precedence reference
  • #UNDEFINE — deleting an existing constant
  • #ifdef — testing whether a constant is defined
  • #script — reading and setting constants at compile time, as used above