Data Types
Named data types may be declared in the "declare" block of a program or procedure, and must appear before any actual variables are declared. The general syntax for a named type declaration is:
|
type <name> = <type>;
type <name> = array [<a>..<b>] of <type>; |
The <name> may be any name starting with a letter, and containing only letters or digits or underscore after the first letter. Names are case sensitive. If an array is specified, the starting and ending subscripts <a> and <b> must be specified, and <b> must be greater than <a> (and both must be literal numbers).
The <type> may be any valid simple type, or may be a record declaration with a field list. Types recognized by PL/i are listed below. Be sure to observe the note about type casting under Assignment Statement further down.
|
int
|
Integer stored as 32-bit signed integer
|
|
float
|
IEEE 754 floating point, 32-bit format
|
|
int16
|
Integer stored as 16-bit signed integer
|
|
uint16
|
Integer stored as 16-bit unsigned integer
|
|
int8
|
Integer stored as 8-bit signed integer |
|
uint8
|
Integer stored as 8-bit unsigned integer |
|
boolean
|
Boolean having a value of 0 or 1, stored as 8-bit unsigned integer |
|
bit
|
Bit having a value of 0 or 1, same as boolean if variable,
packed into 8-bit groups if record field |
|
record
|
Byte packed list of fields comprised of above types (see below) |
Note that 8 bits are stored as 8 bytes when declared as independent variables, but 8 bits will be stored in a single byte when they are fields within a record. It should also be noted that integer values less than 32 bits are processed internally as interim 32-bit data values and truncated only when stored to declared variables. Therefore, the following combination is a legal bounds check:
An important note about type casting: Variables may be any of the types noted above. However, math functions only operate on integer or floating point. If you mix types such as uint8, int16, etc., in a mathematical expression, you will get an "Unexpected types in assignment" error message. Any time you wish to assign a variable to a different type of variable, or do math with anything other then integer or floating point, you must first convert the data to "int" or "float" using the int and float functions.
In the following example, the first four assignment statements will work, but the last one will cause an "Unexpected types" error. The target of the assignment may be any type, but the operands of the expression must be integer or floating point.
What's wrong with the last instance? The operand is not "int" or "float". Either "int" or "float" may be assigned to any type variable. Any other type variable may not be an operand (thus may not be assigned to another variable).