Initialization of variables in ST

Syntax
name_1, name_2 : data-type := literal;

Meaning

→assignment of an →initial value in the context of a declaration of variables
You can initialize internal →variables, →input variables, →output variables and →global variables within the declaration of a →program, of a →function block or of a →function.

The expression on the right side of the assignment operator ":=" may be a →constant expression (in compliance with the →data type).
Here the most important language constructs that you are able to use:

  • a Boolean →literal

    Possible Boolean literals

    Equivalents for TRUE"

    Equivalents for "FALSE"

    BOOL#TRUE

    BOOL#FALSE

    BOOL#1

    BOOL#0

    TRUE

    FALSE

    1

    0

  • a →bit string literal

    Examples for bit string literals

    Description

    Examples

    integer

    0, 123_4, +986

    base 2 (binary literal)

    2#1111_1111 (255 decimal), 2#1110_0000 (224 decimal)

    base 8 (octal literal)

    8#377 (255 decimal), 8#340 (224 decimal)

    base 16 (hexadecimal literal)

    16#FF or 16#ff (255 decimal), 16#E0 or 16#e0 (224 decimal)

    above possibilities as typed literal of type BYTE, WORD, DWORD or LWORD
    (if the value is positioned within the range of values for the specific type)

    WORD#986, WORD#16#FF (255 decimal)

    BOOL is a bit string data type. But there are specific →Boolean literals.

  • an →integer literal

    Examples for integer literals

    Description

    Examples

    integer

    -12, 0, 123_4, +986

    base 2 (binary literal)

    2#1111_1111 (255 decimal), 2#1110_0000 (224 decimal)

    base 8 (octal literal)

    8#377 (255 decimal), 8#340 (224 decimal)

    base 16 (hexadecimal literal)

    16#FF or 16#ff (255 decimal), 16#E0 or 16#e0 (224 decimal)

    above possibilities as typed literal of type SINT, INT, DINT, LINT, USINT, UINT, UDINT, ULINT
    (if the value is positioned within the range of values for the specific type)

    INT#-12, INT#16#FF (255 decimal)

  • a →real literal of data type REAL or LREAL

    Examples for real literals

    Description

    Examples

    Real literal

    0, 0.0, 0.4560, 3.14159_26

    Real literal with exponent

    -1.34E-12, -1.34e-12
    1.0E+6, 1.0e+6, 1E+6
    1.234E6, 1.234e6

    above possibilities as typed literal of type REAL or LREAL
    (if the value is positioned within the range of values for the specific type)

    REAL#0.0, REAL#1.0E+6

  • a →time literal

    Examples for duration literals

    You must enter duration data, such as days (d), hours (h), minutes (m), seconds (s) and fractions of a second (ms for milliseconds, us for microseconds, ns for nanoseconds) or any combinations of them, as shown in the following table. You can separate the duration units by underscore character. Moreover, you can enter the units in upper- or lowercase letters, for example s or S for seconds.

    Description

    Examples

    Duration literals without underscore

    Short prefix

    T#14ms, T#14.7s, T#14.7m, T#14.7h, T#14.7d, T#14h12m, t#5d14h12m18s3.5ms

    Long prefix

    TIME#14ms, time#14h12m

    Duration literals with underscore

    Short prefix

    T#14h_12m, t#5d_14h_12m_18s_3.5ms

    Long prefix

    TIME#14h_12m, time#5d_14h_12m_18s_3.5ms

    Observe that your used →target system might influence the usage of duration literals in logi.CAD 3 (see "Properties and restrictions specific to the target system").

    Examples for date and time of day literals

    In case of date and time literal and time of day literal, the fractions of a second (entered after the .) are optional.

    Description

    Examples

    Date literal

    Short prefix

    D#1984-06-25

    Long prefix

    DATE#1984-06-25

    Date and time literal

    Short prefix

    DT#1984-06-25-15:36:55.360_227_400

    Long prefix

    DATE_AND_TIME#1984-06-25-15:36:55.360227400

    Time of day literal

    Short prefix

    TOD#15:36:55.36

    Long prefix

    TIME_OF_DAY#15:36:55.36

    Observe that your used →target system might influence the usage of these literals in logi.CAD 3 (see "Properties and restrictions specific to the target system").

  • a →character string literal

    Examples for character string literals

    Single-byte character strings consist of zero or more characters prefixed and terminated by the single quote character '.
    In contrast, logi.CAD 3 does not support double-byte character strings enclosed in the double quote character " for character string literals.

    Single-byte character strings are:

    Description

    Examples

    STRING literal

    'OK', 'ABCDEF'

    typed STRING literal

    STRING#'OK', STRING#'ABCDEF'

    CHAR literal

    'B', 'T'

    typed CHAR literal

    CHAR#'B', CHAR#'T'

    Three-character combination of the dollar sign ($) followed by two hexadecimal digits are interpreted in logi.CAD 3 as the corresponding character of the ANSI C character set.

    This enables you to enter special characters, such as umlauts, for single-byte character string. Currently, such special characters are displayed in logi.CAD 3 (e.g. in the der Values of Variable view) as they have been entered.
    Here a selection of special characters (a list of special characters and the corresponding hexadecimal digits is listed under https://www.ascii-code.com/):

    Decimal value

    Hexadecimal digit

    To enter

    Special character

    128

    80

    $80

    163

    A3

    $A3

    £

    169

    A9

    $A9

    ©

    174

    AE

    $AE

    ®

    177

    B1

    $B1

    ±

    178

    B2

    $B2

    ² (superscript "2")

    179

    B3

    $B3

    ³ (superscript "3")

    188

    BC

    $BC

    ¼

    189

    BD

    $BD

    ½

    190

    BE

    $BE

    ¾

    196

    C4

    $C4

    Ä

    214

    D6

    $D6

    Ö

    220

    DC

    $DC

    Ü

    223

    DF

    $DF

    ß

    228

    E4

    $E4

    ä

    246

    F6

    $F6

    ö

    252

    FC

    $FC

    ü

    If the character combination '$00' is assigned to a single character of a STRING variable, the character string of the STRING variable is terminated at this position. Such character strings are terminated in case of $00 in the Values of Variables view as well.
    However, the complete character string is copied in assignments. This makes it possible to have the Values of Variables view display single characters behind $00, if you access the assigned characters behind $00.

    Moreover, the following specifications are possible for single-byte character strings:

    Specification

    Meaning

    ''

    empty string

    ' '

    string with space character

    '$''

    string with single quote character '

    '"'

    string with double quote character "

    Moreover, the following specifications are possible for two-character combinations in single-byte character strings:

    Specification

    Meaning

    $'

    string with single quote character '

    $$

    string with dollar sign $

    $L oder $l

    string with line feed

    $N oder $n

    string with newline

    $P oder $p

    string with form feed (page)

    $R oder $r

    string with carriage return

    $T oder $t

    string with tabulator

  • a →named element of a data type with named values

  • structure initialization (initialization of structure elements)

  • operators (such as +, -, *, /)

Initialize array variables similar to the declaration of an array data type.
See "Declaration of reference variables (incl. assignments to them)", if you need information on how to initialize reference variables.

Example
VAR CONSTANT
PI : REAL := 3.141592;
END_VAR
VAR
var1 : REAL := 3.141592;
var2 : DINT := 4711;
END_VAR
VAR
var3 : INT := TrafficLight#Red;
(* initializing the variable 'var3' with the named element 'Red' of the data type 'TrafficLight' *)
END_VAR
VAR
VRange : Range := (min := -5, max := 5); 
(* structure initialization for variable 'VRange' of structured data type 'Range': initializing the sub-elements 'min' and 'max' *)
END_VAR