Initial Values

Variables can be assigned initial values in their declaration using the := operator.

IEC 61131-3

Section 2.4.3

Support

Supported

Syntax

variable_name : type_name := initial_value ;

The initial value must be a constant expression compatible with the variable’s type.

Example

PROGRAM main
    VAR
        counter : INT := 0;
        active : BOOL := TRUE;
        scale : REAL := 1.5;
    END_VAR

    counter := counter + 1;
END_PROGRAM

If no initial value is specified, the variable is initialized to the default value of its type (typically zero or empty).

Named Constants

The initial value may name a variable declared with the CONSTANT qualifier, or be an expression over literals and such constants. The compiler replaces the name with the constant’s value when it compiles the program, so the variable starts at that value. A STRING or WSTRING variable and the default of a TYPE declaration take a constant’s name the same way.

ironplcc check --allow-constant-initializer-expressions main.st

Pass the --allow-constant-initializer-expressions flag to enable it, or select a dialect that includes it. See Enabling Dialects and Features for the dialects and flags reference.

PROGRAM main
    VAR CONSTANT
        LIMIT : INT := 100;
        NAME : STRING := 'pump';
    END_VAR
    VAR
        threshold : INT := LIMIT;
        label : STRING[20] := NAME;
    END_VAR

    threshold := threshold - 1;
END_PROGRAM

The constant’s declared type must be compatible with the variable’s type by the same rule as an assignment: an INT constant may initialize a DINT variable, but a UDINT constant initializing an INT variable is reported as P4022. A name that is not a constant is reported as P4038, and this form without the flag as P4037.

See Also