Constant Expressions
Constant expressions can be used in all languages for assigning a variable with a value.
All constant expressions have well-defined Data Types according to their semantics.
-
- If you program an operation between variables and constant expressions having inconsistent data types, it leads to syntactic errors when the program is compiled.
These are the syntactic rules for constant expressions according to possible data types:
Type |
Prefix |
Description |
||||||||||||||||||
|
Boolean
|
|||||||||||||||||||
|
32-bit (default) Integer
|
|||||||||||||||||||
INT# |
16-bit Integer
|
|||||||||||||||||||
LINT# |
Long (64-bit) Integer
|
|||||||||||||||||||
LREAL# |
Double Precision Floating Point Value
|
|||||||||||||||||||
|
Single Precision Floating Point Value
|
|||||||||||||||||||
SINT# |
Small (8-bit) Integer
|
|||||||||||||||||||
|
Character String
|
|||||||||||||||||||
T# or TIME# |
Time of Day
|
|||||||||||||||||||
UDINT# |
Unsigned 32-bit Integer
|
|||||||||||||||||||
UINT# |
Unsigned 16-bit Integer
|
|||||||||||||||||||
ULINT# |
Unsigned Long Unsigned (64-bit) Integer
|
|||||||||||||||||||
USINT# |
Unsigned 8-bit Integer
|
Examples
Valid Constant Expressions
These are examples of valid constant expressions.
Constant Expression |
Type |
Description |
---|---|---|
'hello' |
Character String |
Character string. |
'I$'m here' |
Character String |
Character string with a quote inside (I'm here). |
'name$Tage' |
Character String |
Character string with two words separated by a tab. |
'x$00y' |
Character String |
Character string with two characters separated by a null character (ASCII code 0). |
0.0 |
REAL |
0 expressed as a REAL number. |
1.002E3 |
REAL |
1002 expressed as a REAL number in scientist format. |
2#1000100 |
DINT |
DINT integer in binary basis. |
8#34712 |
DINT |
DINT integer in octal basis. |
16#abcd |
DINT |
DINT integer in hexadecimal basis. |
123456 |
DINT |
DINT (32-bit) integer. |
FALSE |
BOOL |
FALSE Boolean expression. |
INT#2000 |
16-bit Integer |
16-bit integer. |
LINT#1 |
Long (64-bit) Integer |
Long (64 bit) integer having the value 1. |
LREAL#1E-200 |
Double Precision Floating Point Value |
Double precision real number. |
SINT#127 |
Small (8-bit) Integer |
Small integer. |
T#1h123ms |
Time of Day |
TIME value with some units missing. |
T#23h59m59s999ms |
Time of Day |
Maximum TIME value. |
TIME#0s |
Time of Day |
Null TIME value. |
TRUE |
BOOL |
TRUE Boolean expression. |
Invalid Constant Expressions
These are examples of errors in constant expressions:
Invalid Constant Expressions |
Description |
---|---|
'I'm here' |
Quote within a string with "$" mark omitted. |
1a2b |
Basis prefix ("16#") omitted. |
1E-200 |
"LREAL#" prefix omitted for a double precision float. |
BooVar := 1; |
0 and 1 cannot be used for Booleans. |
hello |
Quotes omitted around a character string. |
T#12 |
Time unit missing. |
-
- There are pre-defined constants.
See Use the Defines List, Internal Defines, and Global Defines for more information.