Previous Topic Next topic Print topic


Constants

A constant is a sequence of characters that represents a particular, unchanging value. In Open PL/I, five kinds of constants are permissible:

Examples:

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."""
Note:

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.

The XU constant hexadecimal value is the value specified, padded on the left with hexadecimal zeros if needed. For example:
'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.

Example:

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

Previous Topic Next topic Print topic