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

BOOL

 

Boolean

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.
  • Use 2#, 8#, or 16# prefixes to specify an integer in binary, octal or hexadecimal basis, respectively.
  • See Valid Constant Expressions for an example.

INT

INT#

16-bit Integer

  • 16-bit integer constant expressions are valid integer values (between -32768 and +32767).
  • Must be prefixed with INT#.
  • All integer expressions having no prefix are considered DINT integers.
  • See Valid Constant Expressions for an example.

LINT

LINT#

Long (64-bit) Integer

  • Long integer constant expressions are valid integer values.
  • Must be prefixed with LINT#.
  • All integer expressions having no prefix are considered DINT integers.
  • See Valid Constant Expressions for an example.

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 (dot zero) 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 are lost, leading to a loss of precision.
  • See Valid Constant Expressions for an example.

REAL

 

Single Precision Floating Point Value

  • REAL is the default precision for floating points: such expressions do not require a prefix.
  • Important: REAL is restrictive, but because it is the default, it is recommended to explicitly declare your real constants with the LREAL# prefix.
  • 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 (dot zero) at the end of the number.
  • You can use F or E separators for specifying the exponent in case of a scientific representation.
  • REAL constants are limited to 6-7 digits of accuracy.
    • Any digits after these significant digits are lost, leading to a loss of precision.
  • See Valid Constant Expressions for an example.

SINT

SINT#

Small (8-bit) Integer

  • Small integer constant expressions are valid integer values (between -128 and +127).
  • Must be prefixed with SINT#.
  • All integer expressions having no prefix are considered DINT integers.
  • See Valid Constant Expressions for an example.

STRING

 

Character String

  • String expressions must be written between single quote marks ('  ').
  • The length of the string cannot exceed 255 characters.
  • See Valid Constant Expressions for an example.
  • Use these sequences to represent a special or not-printable character within a string:

Sequence

Description

$$

A "$" character

$'

A single quote

$T

A tab stop (ASCII code 9)

$R

A carriage return character (ASCII code 13)

$L

A line feed character (ASCII code 10)

$N

Carriage return plus line feed characters (ASCII codes 13 and 10)

$P

A page break character (ASCII code 12)

$xx

Any character (xx is the ASCII code expressed on two hexadecimal digits

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 T# or TIME#.
  • They are expressed as a number of:
    • hours followed by h
    • minutes followed by m
    • seconds followed by s
    • milliseconds followed by ms
    • The order of units (hour, minutes, seconds, milliseconds) must be respected.
      • Blank characters are not allowed in the time expression.
      • There must be at least one valid unit letter in the expression.
      • See Valid Constant Expressions for an example.

UDINT/DWORD

UDINT#

Unsigned 32-bit Integer

  • Unsigned 32-bit integer constant expressions are valid integer values (between 0 and 4294967295).
  • Must be prefixed with UDINT#.
  • All integer expressions having no prefix are considered DINT integers.

UINT/WORD

UINT#

Unsigned 16-bit Integer

  • Unsigned 16-bit integer constant expressions are valid integer values (between 0 and +65535).
  • Must be prefixed with UINT#.
  • All integer expressions having no prefix are considered DINT integers.

ULINT/LWORD

ULINT#

Unsigned Long Unsigned (64-bit) Integer

  • Unsigned 64-bit integer constant expressions are valid integer values.
  • All integer expressions having no prefix are considered DINT integers.

USINT/BYTE

USINT#

Unsigned 8-bit Integer

  • Unsigned small integer constant expressions are valid integer values (between 0 and 255).
  • Must be prefixed with USINT#.
  • All integer expressions having no prefix are considered DINT integers.

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.