Constant Expressions
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:
Type | Prefix | Description | ||||||||||||||||||
BOOL | Boolean. There are only two possible Boolean constant expressions. They are
reserved keywords TRUE and FALSE . |
|||||||||||||||||||
SINT | 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 | USINT# | 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 | 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 | UINT# | Unsigned 16 bit Integer. Unsigned 16 bit integer constant expressions are valid integer values (between 0 and +65535) 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 to specify an integer in binary, octal or hexadecimal basis respectively. |
|||||||||||||||||||
UDINT/DWORD | UDINT# | 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 | 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. | ||||||||||||||||||
ULINT/LWORD | ULINT# | Unsigned 64-bit integer constant expressions are valid integer values. All integer expressions having no prefix are considered as DINT integers. | ||||||||||||||||||
REAL |
Single precision floating point value. Real constant expressions must be valid numbers, 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 scientific representation. REAL is the default precision for floating points: such expressions do not require a prefix. REAL is restrictive, but because it is the default, it is recommended to explicitly declare your real constants with the LREAL# prefix. REAL constants are limited to 6-7 digits of accuracy. Any digits after these significant digits will be lost, leading to a loss of precision. |
|||||||||||||||||||
LREAL | LREAL# |
Double precision floating point value. Real constant expressions must be valid numbers, 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 scientific representation. LREAL constants are limited to 14-15 digits of accuracy. Any digits after these significant digits will be lost, leading to a loss of precision. |
||||||||||||||||||
TIME | T# or 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".
|
||||||||||||||||||
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:
|
Table 5-1: List of Prefixes for Constant expressions
Examples
Below are some examples of valid constant expressions:
TRUE | TRUE Boolean expression |
FALSE | FALSE Boolean expression |
SINT#127 | small integer |
INT#2000 | 16 bit integer |
123456 | DINT (32 bit) integer |
16#abcd | DINT integer in hexadecimal basis |
8#34712 | DINT integer in octal basis |
2#1000100 | DINT integer in binary basis |
LINT#1 | long (64 bit) integer having the value "1" |
0.0 | 0 expressed as a REAL number |
1.002E3 | 1002 expressed as a REAL number in scientist format |
LREAL#1E-200 | Double precision real number |
T#23h59m59s999ms | maximum TIME value |
TIME#0s | null TIME value |
T#1h123ms | TIME value with some units missing |
'hello' | character string |
'name$Tage' | character string with two words separated by a tab |
'I$'m here' | character string with a quote inside (I'm here) |
'x$00y' | 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; | 0 and 1 cannot be used for Booleans |
1a2b | basis prefix ("16#") omitted |
1E-200 | "LREAL#" prefix omitted for a double precision float |
T#12 | Time unit missing |
'I'm here' | quote within a string with "$" mark omitted |
hello | quotes omitted around a character string |
Additionally, there are pre-defined constants. See Use the Defines List for information about Internal and user-defined Defines.