Operands
Constants and literals
Constants are identifiers for unchanging values. You can declare constants locally within a programming block or globally within a global variable list. The declaration section is extended with the keyword CONSTANT for this purpose.
Constants are also strings that represent the value of a base type such as integers or floating-point numbers, for example 16#FFFF_FFFF, T#5s or -1.234 E-5. To distinguish them, such constants are also called literals, literal constants, or unnamed constants. There are logical (TRUE, FALSE) or numeric literals (3.1415, T#5s), but also character literals ('Hello world!', "black").
Syntax declaration:
<scope> CONSTANT
<identifier>:<data type> := <initial value>;
END_VAR
<scope> : VAR | VAR_INPUT | VAR_STAT | VAR_GLOBAL
<data type> : <elementary data type | user defined data type | function block >
<initial value> : literal value | identifier | expression
Allowed initial values:
- Literal, for example TRUE, FALSE, 16#FFFF_FFFF
- Named constant declared elsewhere.
- Simple expression from literals, also combined with simple operators such as + - *.
Inputs or function calls cannot be specified as initial values.
Sample:
VAR_GLOBAL CONSTANT
cMax : INT := 100;
cSpecial : INT := cMax - 10;
END_VAR
Constants are only described in the declaration. The assignment of an initial value is mandatory. Within an implementation, constants are read only and therefore always appear to the right of the allocation operator in an instruction.
The constants are replaced with the initial value when the code is compiled. It must also be possible to calculate the initial value at compile time.
Constants of structured or user-defined types are calculated only at runtime. Structured constants in programs or GVLs are calculated once at program start. Structured constants in functions or methods are calculated each time the function or method is called. The initialization of structured constants can thus depend on inputs or execute function calls.
See also:
Variables
You can declare variables either locally in the declaration part of a function block, or in a global variable list.
At which point you can use a variable depends on its data type.
See also:
Further
See also: