An unnamed numeric constant has the type UNIVERSAL, which is a 32-bit signed value. When a value of type UNIVERSAL is used in an operation, it is converted to the type of the other operand.
Numeric constants have the following formats:
| 12 -- decimal |
| 0x12 -- hexadecimal |
| 0b01 -- binary |
| 0q01 -- octal |
| "a" -- ASCII |
For example:
VAR BYTE ch = "123" ' ch is set to '1'
VAR BYTE str[] = "123" ' str[0] is set to '1'
' str[1] is set to '2'
' str[2] is set to '3'
An ASCII constant allows the C language escaping rules as follows:
Table 2-2. ASCII Constant Escaping
| Sequence | Value |
|---|---|
| "\0qqq" | octal constant |
| "\a" | bell |
| "\b" | backspace |
| "\f" | formfeed |
| "\n" | line feed |
| "\r" | carriage return |
| "\t" | horizontal tab |
| "\v" | vertical tab |
| "\xdd" | hexidecimal constant |
| "\\" | A single '\' |
constants other than ASCII constants may also contain any number of underscores ("_") which are ignored, but are useful for grouping. For example: 0b0000_1111
The complete format for defining a named constant is:
CONST [type[*cexpr]] identifier [ '[' [ cexpr ] ']' ] '=' { cexpr | '{' cexpr1[',' cexpr2...]'}' | '"'...'"'} [ ',' identifier2...]
CONST denotes the beginning of a constant definition clause.
Defines the type of the constant. If none is given, the constant becomes universal type which is 32 bit signed.
Defines a constant array (see array variable types). A constant array will not take any space unless it is indexed at least once with a non-constant subscript. On the PIC, constant arrays consume *code* space, not *data* space, and are limited to 255 elements.
If cexpr is ommitted, the size of the array will be determined by the number of initializers used.
For non-array constants this assigns the value to the constant
For arrays of constants this assigns the value to each element. There must be the same number of cexprs as there are elements defined.
For an array of constants, this assigns each ASCII value between '"' and '"' to one element of the constant array. Unlike C, there is no terminating NUL.