DATA

The DATA adds numeric values to the data or code segment of the program.

Syntax

DATA AS <type> <value> [, <value> ...]

The DATA statement in XC=BASIC is different from the one in CBM BASIC. In XC=BASIC, there's no READ command to copy data into variables but DATA is typically used together with DIM to statically initialize a variable with values, like in the following example:

DIM squares(7) AS INT @lab_squares
PRINT squares(3) : REM -- outputs 9
lab_squares:
DATA AS INT 0, 1, 4, 9, 16, 25, 36

The DIM statement in the first line defines the variable squares as an array of seven integers and tells the compiler that the address of the array is where the lab_squares label is. Therefore the array is already filled with the values after the DATA statement without the need of copying using READ.

The --inline-data option

By default, data are separated from code in XC=BASIC. This means that anything defined in DATA statements will be placed in a separate segment that is located after the code segment, outside of the program flow. This means that you can place a DATA statement anywhere, it won't affect the generated program code. The following example is identical to the one above.

lab_squares:
DATA AS INT 0, 1, 4, 9, 16, 25, 36
DIM squares(7) AS INT @lab_squares
PRINT squares(3) : REM -- outputs 9

There are situations, however, when you don't want to separate data from code and you need full control over where data is compiled (e. g using the ORIGIN directive). This is where the command line option --inline-data (or shortly -i) comes in handy. Setting it to TRUE will make DATA statements compiled right where they're in the program flow. For example:

' To be compiled with the option --inline-data=true
GOTO main
lab_squares:
DATA AS INT 0, 1, 4, 9, 16, 25, 36
main:
DIM squares(7) AS INT @lab_squares
PRINT squares(3) : REM -- outputs 9

If the above example is compiled using the option --inline-data=true, data will precede the main program.

Warning

When data is compiled inline, you have to make sure that DATA statements never get executed as part of the program flow. Note the GOTO command above that skips DATA. If data gets executed by mistake, the program will (most likely) crash.