INT / DINT / REAL / TIME · 7 min · Free exercise
IEC data types: BOOL, INT, DINT, REAL and TIME
IEC elementary data types
Every variable has a type that decides what it can hold: a yes-or-no value, a whole number, a number with a fraction, or a length of time. Choosing the right type is the first decision in any IEC program.
What it does exactly
BOOL holds TRUE or FALSE. The whole-number types are SINT, INT, DINT and LINT, which are 8, 16, 32 and 64 bits and signed, with unsigned versions named USINT, UINT, UDINT and ULINT. REAL and LREAL hold floating-point numbers. TIME holds a duration, written with a literal such as T#3s or T#500ms.
Dividing two whole numbers gives a whole number: the fraction is dropped, not rounded. A reading of 253 tenths of a degree divided by 10 gives 25. To keep the fraction, convert to REAL first. Mixing types is normally done with an explicit conversion function, such as INT_TO_REAL.
The IEC and editor equivalent
These are the IEC types themselves. In this editor declare them in a VAR block, such as TEMP_C AT %QW0 : INT;, and use DIV(a, b) for division.
What the scan does with it
A variable keeps its value from one cycle to the next unless the program changes it, so a type that is too small for the value silently wraps or limits. The cycle does not check the range for you.
VAR TEMP_DECI AT %IW0 : INT; TEMP_C AT %QW0 : INT; END_VAR TEMP_C := DIV(TEMP_DECI, 10);
Common mistakes
- Expecting integer division to round. 9 divided by 10 as INTs is 0.
- Storing a 32-bit value in a 16-bit variable and losing the high part.
What you can now do: You can pick a data type for a value and predict what integer division does to it.