A constant is a sequence of characters that represents a particular, unchanging value. In Open PL/I, five kinds of constants are permissible:
These are actual numbers or strings written in the source program. Literal constants are restricted to character strings, bit strings, and fixed- or floating-point numbers.
These are established using a label prefix in the source program that declares a name either as the name of a format or a procedure, or as a statement label. Label constants cannot be declared in a DECLARE statement.
These constants are typically established by DECLARE statements. File constants are identifiers declared with the FILE attribute to describe the type of input/output to be performed on a file. Entry constants are used to invoke procedures at certain entry points.
These are identifiers that are to be changed with the %REPLACE statement. Like literal constants, constant identifiers can also be character strings, bit strings, and fixed- or floating-point decimal numbers.
These constants are scalar identifiers that are declared by the BYVALUE attribute. They can be declared for arithmetic data, string data, and for pointers and offsets.
Constant | Type |
---|---|
25 | Integer constant |
7.5 | Fixed-point decimal constant |
3.12E-01 | Floating-point decimal constant |
'How are you?' | Character-string constant |
'1'B | Bit-string constant (binary notation) |
'1011'B | Bit-string constant (binary notation) |
'775'B3 | Bit-string constant (octal notation) |
'A7O'B4 | Bit-string constant (hexadecimal notation) |
'100'XN | Binary fixed-point (XN) constant (hexadecimal notation) |
STARTUPD: | Label constant |
%REPLACE TABLE_SIZE BY 350; | TABLE_SIZE is an identifier to be replaced by an integer constant |
DECLARE E ENTRY; | E is an entry constant |
DECLARE F FILE; | F is a file constant |
For a table showing the expansion of nonbinary notation into binary notation, see the section Bit-String Data.
Arithmetic constants represent decimal values, whereas binary arithmetic values have no constant representation. Nevertheless, any decimal constant can be converted easily to a binary value by using it in a context that expects a binary arithmetic value.
As a result of Open PL/I language rules, all constants that appear as floating-point numbers in a source program are actually of type float decimal and are subject to the rules of float decimal arithmetic.
A character-string constant can contain any character, except that, if an apostrophe is required within a character-string constant, it is written as two apostrophes. For example,
'He said, "I''m fine."'
Open PL/I also supports string constants delimited by double-quotes ("). For example:
"He said, ""I'm fine."""
The double quotation mark character is not equivalent to a single quotation mark or two adjacent single quotation marks and has no more significance than any other character within a constant.
In Open PL/I, the maximum size of a string constant is 256 characters or bits (for details, see the section Open PL/I Compiler Implementation Limits). The Compiler checks this limit after bit-string constants written in the B2, B3, or B4 format have been expanded. Refer to the data type descriptions in the chapter Language Concepts in your Open PL/I User's Guide for the maximum size limits for floating- and fixed-point data.
A named constant is similar to a scalar declaration with the appropriate data attributes, plus the BYVALUE attribute. The BYVALUE attribute signifies this identifier is a named constant, with all of the attributes taken as applied to the declaration.
Named constants differ from simple constants in that they retain the attributes from the declaration. For example,
dcl i fixed bin (15) byvalue(1); dcl j fixed bin (15); J = 1; /* 1 is fixed dec, requires conversion */ J = I; /* I is fixed bin (15), no conversion (integer 1 assigned, not "i" ) */
For string data items, the length is determined from the length of the string value when the length is not specified.
declare string char value ('abcdef') ; /* string is char(6) */
An XN constant is a SIGNED fixed binary constant in hexadecimal notation. An XN constant with eight or fewer digits has a precision of 31. An XN constant with more than eight digits has a precision of 63.
The XN constant hexadecimal value is the value specified, padded on the left with hexadecimal zeros if needed. For example:
'102'XN /* same as '00000102'XN with value 258 */ '9C40'XN /* same as '00009C40'XN with value 40,000 */ '7FFF'XN /* same as '00007FFF'XN with value 32,767 */
An XU constant is an UNSIGNED fixed binary constant in hexadecimal notation. An XU constant with eight or fewer digits has a precision of 32. An XU constant with more than eight digits has a precision of 64.
'102'XU /* same as '00000102'XU with value 258 */ '9C40'XU /* same as '00009C40'XU with value 40,000 */ 'FFFF'XU /* same as '00007FFF'XU with value 65,535 */
In Open PL/I, named constants can be declared as arithmetic or string; pointers and offsets are not yet supported. Named constants can be declared before or after being used, and the same scoping rules as any declaration apply.
cool: proc options(main); dcl pname char (16) byvalue (procedurename()); dcl flag bit(1) aligned static init(true); dcl true bit(1) byvalue ('1'b); dcl false bit(1) byvalue('0'b); dcl myarr (n) fixed bin (15); dcl n fixed bin(15) byvalue (800); dcl arr (k/3) fixed bin (15) static init (k+1, k, k+3); dcl arr1 (100+100) fixed bin (15); dcl arr2 (n+100) fixed bin (15); dcl i fixed bin (15); dcl k fixed bin (15) byvalue (9); if flag then put skip list ('Named Constants are ', pname); put skip list(arr); i = n + 100; arr1 (k) = 99; put skip list (size(arr1), size(arr2), i, arr1(K*2-K)); put skip list(arr); end; Named Constants are COOL 10 9 12 400 1800 900 99 10 9 12
You can punctuate arithmetic, bit, and hexadecimal constants with the break character ( _ ) to improve readability. For example:
'1100_1111'B is the same as '11001111'B
'F_E'B4 is the same as 'fe'b4
16_666_667 is the same as 16666667