Dim

Syntax:

    For Variables > 1 byte:
    Dim variable[, variable2 [, variable3]] [As type] [Alias othervar [, othervar2]]

    'or

    Dim variable[, variable2 [, variable3]] [As type] [At location]


    For Arrays:
    Dim array(size) [As type] [At location]

    For String:
    Dim string [* _size] [At location]

Explanation:

Dim has two uses:  It can be used to define 1) variables of many types and 2) arrays.

Command Availability:

Available on all microcontrollers.

The Dim variable command is used to instruct GCBASIC to allocate variables or to create alternate names for existing variables (using Alias) or to create variables at a specific memory location (using At).

The Dim array command also sets up array variables. The maximum array size is determined by the parameter size is dynamically allocated by the compiler and depends on the specific chip used, as well as the complexity of the program.

The limit on array size varies dependent on the chip type. See the Maximum Array Size section in Arrays for more information.

type specifies the type of variable that is to be created. Different variable types can hold values over different ranges, and use different amounts of RAM. See the Variables Types article for more information.

When multiple variables are included on the one line, GCBASIC will set them all to the type that is specified at the end of the line. If there is no type specified, then GCBASIC will make the variable a byte.

Alias creates a variable using the same memory location as one or more other variables.   It is mainly used internally in GCBASIC to treat system variables as a word.   For example, this command is used to create a word variable, made up from the two memory locations used to store the result of an A/D conversion.  `Alias` is mutually exclusive to At and therefore Alias and At on the same declaration line will cause an compiler error.

AT a variable can be placed at a specific location in the data memory of the chip using the At option.   `location` will be used whether it is a valid location or not, but a warning will be generated if GCBASIC has already allocated the memory, or if the memory does not appear to be valid.   This can be used for peripherals that have multi byte buffers in RAM.  `At` is mutually exclusive to Alias and therefore At and Alias on the same declaration line will cause an compiler error.

A String declared with a fixed size (numeric constant that can be evaluated at compile time) is a fixed length string. It has no descriptor and it is not resized to fit its contents. If data overflows the size of the string, the memory may be overwrtten. Fixed length strings are not NULL terminated, and they use size + 1 bytes of space. String variable names need not end in a dollar sign $ as in other dialects of BASIC.

    Dim ADResult As Word Alias ADRESH, ADRESL

Example:

    'This program will set up a array of ten bytes and a word variable

    dim DataList(10)
    dim Reading as word

    Reading = 21978
    DataList(1) = 15

    dim stringvariable as string

For more help, see:  SerPrint articles as these articales show how to use Dim to create string variables and Variables for more details in creating and managing strings lengths.