Constant expressions can be used in all languages for assigning a variable with a value. All constant expressions have a well defined data type according to their semantics. If you program an operation between variables and constant expressions having inconsistent data types, it will lead to syntactic errors when the program is compiled. Below are the syntactic rules for constant expressions according to possible data types:
BOOL: Boolean
There are only two possible boolean constant expressions. They are reserved keywords TRUE and FALSE.
SINT: Small (8 bit) Integer
Small integer constant expressions are valid integer values(between -128 and 127) and must be prefixed with "SINT#'. All integer expressions having no prefix are considered as DINT integers.
USINT / BYTE: Unsigned 8 bit Integer
Unsigned small integer constant expressions are valid integer values(between 0 and 255) and must be prefixed with "USINT#'. All integer expressions having no prefix are considered as DINT integers.
INT: 16 bit integer
16 bit integer constant expressions are valid integer values(between -32768 and 32767) and must be prefixed with "INT#'. All integer expressions having no prefix are considered as DINT integers.
UINT / WORD: Unsigned 16 bit integer
Unsigned 16 bit integer constant expressions are valid integer values(between 0 and 255) and must be prefixed with "UINT#'. All integer expressions having no prefix are considered as DINT integers.
DINT: 32 bit (default) integer
32 bit integer constant expressions must be valid numbers between -2147483648 to +2147483647. DINT is the default size for integers: such constant expressions do not need any prefix. You can use "2#", "8#" or "16#" prefixes for specifying a number in respectively binary, octal or hexadecimal basis.
UDINT / DWORD: Unsigned 32 bit integer
Unsigned 32 bit integer constant expressions are valid integer values(between 0 and 4294967295) and must be prefixed with "UDINT#'. All integer expressions having no prefix are considered as DINT integers.
LINT: Long (64 bit) integer
Long integer constant expressions are valid integer values and must be prefixed with "LINT#'. All integer expressions having no prefix are considered as DINT integers.
REAL: Single precision floating point value
Real constant expressions must be valid number, and must include a dot ("."). If you need to enter a real expression having an integer value, add ".0" at the end of the number. You can use "F" or "E" separators for specifying the exponent in case of a scientist representation. REAL is the default precision for floating points: such expressions do not need any prefix.
LREAL: Double precision floating point value
Real constant expressions must be valid number, and must include a dot ("."), and must be prefixed with "LREAL#". If you need to enter a real expression having an integer value, add ".0" at the end of the number. You can use "F" or "E" separators for specifying the exponent in case of a scientist representation.
TIME: Time of day
Time constant expressions represent durations that must be less than 24 hours. Expressions must be prefixed by either "TIME#" or "T#". They are expressed as a number of hours followed by "h", a number of minutes followed by "m", a number of seconds followed by "s", and a number of milliseconds followed by "ms". The order of units (hour, minutes, seconds, milliseconds) must be respected. You cannot insert blank characters in the time expression. There must be at least one valid unit letter in the expression.
STRING: Character string
String expressions must be written between single quote marks. The length of the string cannot exceed 255 characters. You can use the following sequences to represent a special or not printable character within a string:
$$ $' $T $R $L $N $P $xx |
a "$"
character a single quote a tab stop (ASCII code 9) a carriage return character (ASCII code 13) a line feed character (ASCII code 10) carriage return plus line feed characters (ASCII codes 13 and 10) a page break character (ASCII code 12) any character (xx is the ASCII code expressed on two hexadecimal digits |
Examples
Below are some examples of valid constant expressions
TRUE FALSE SINT#127 INT#2000 123456 16#abcd LINT#1 0.0 1.002E3 LREAL#1E-200 T#23h59m59s999ms TIME#0s T#1h123ms 'hello' 'name$Tage' 'I$'m here' 'x$00y' |
TRUE
boolean expression FALSE boolean expression small integer 16 bit integer DINT (32 bit) integer DINT integer in hexadecimal basis long (64 bit) integer having the value "1" 0 expressed as a REAL number 1002 expressed as a REAL number in scientist format Double precision real number maximum TIME value null TIME value TIME value with some units missing character string character string with two words separated by a tab character string with a quote inside (I'm here) character string with two characters separated by a null character (ASCII code 0) |
Below are some examples of typical errors in constant expressions
BooVar := 1; 1a2b 1E-200 T#12 'I'm here' hello |
0 and 1
cannot be used for booleans basis prefix ("16#") omitted "LREAL#" prefix omitted for a double precision float Time unit missing quote within a string with "$" mark omitted quotes omitted around a character string |