Subrange types
A sub-range data type is a type whose range of values is only a subset of that of the basic type. The declaration can be carried out in the data types register, but a variable can also be directly declared with a subrange type:
Syntax for the declaration in the 'Data types' register:
TYPE <Name> : <Inttype> (<ug>..<og>) END_TYPE;
Type | Description |
---|---|
<Name> | must be a valid IEC identifier |
<Inttype> | is one of the data types SINT, USINT, INT, UINT, DINT, UDINT, BYTE, WORD, DWORD (LINT, ULINT, LWORD). |
<ug> | Is a constant which must be compatible with the basic type and which sets the lower boundary of the range types. The lower boundary itself is included in this range. |
<og> | Is a constant that must be compatible with the basic type, and sets the upper boundary of the range types. The upper boundary itself is included in this basic type. |
Example:
TYPE
SubInt : INT (-4095..4095);
END_TYPE
Direct declaration of a variable with a subrange type:
VAR
i1 : INT (-4095..4095);
i2: INT (5...10):=5;
ui : UINT (0..10000);
END_VAR
If a constant is assigned to a subrange type (in the declaration or in the implementation) that does not apply to this range (e.g. 1:=5000), an error message is issued.
In order to check for observance of range boundaries at runtime, the functions CheckRangeSigned or CheckRangeUnsigned must be introduced.