Language > Data Types > Defining and Using Constants

# Defining and Using Constants

The syntax of a constant determines its type. Efficiency is adversely affected when the type of a constant must be converted during expression evaluation. Consider the following expression:

A + 5

If the variable A is of floating-point type, the constant 5 must be converted from short integer type to floating point each time the expression is evaluated.

The type of a constant also has an important effect in array expressions. Care must be taken to write constants of the correct type. In particular, when performing arithmetic on byte arrays with the intent of obtaining byte results, be sure to use byte constants; e.g., nB. For example, if A is a byte array, the result of the expression A + 5B is a byte array, while A + 5 yields a 16-bit integer array.

## Boolean Constants

There are two boolean constants: !TRUE, equal to 1, and !FALSE, equal to 0. Both of these constants are variables of type byte with the boolean flag.

## Integer Constants

Numeric constants of different types can be represented by a variety of forms. The syntax used when creating integer constants is shown in the following table, where n represents one or more digits.

Radix | Type | Form | Examples
Decimal | Byte | nB | 12B, 34B
Integer | n or nS | 12,12S,425,425S
Unsigned Integer | nU or nUS | 12U,12US
Long | nL | 12L, 94L
Unsigned Long | nUL | 12UL, 94UL
64-bit Long | nLL | 12LL, 94LL
Unsigned 64-bit Long | nULL | 12ULL, 94ULL
Hexadecimal | Byte | 'n'XB | '2E'XB
Integer | 'n'X or 'n'XS | '0F'X, 'A2'XS
Unsigned Integer | 'n'XU or 'n'XUS | ’0F’XU, 'A2'XUS
Long | 'n'XL | 'FF'XL
Unsigned Long | 'n'XUL | ’FF’XUL
64-bit Integer | 'n'XLL | ’FF’XLL
Unsigned 64-bit Integer | 'n'XULL | 'FF'XULL
Octal | Byte | "nB | "12B
Integer | "n | "12
'n'O or 'n'OS | '377'O, '234'OS
Unsigned Integer | "nU | "12U
'n'OU or 'n'OUS | '377'OU, '234'OUS
Long | "nL | "12L
'n'OL | '777777'OL
Unsigned Long | "nUL | "12UL
'n'OUL | '777777'OUL
64-bit Long | "nLL | "12LL
'n'OLL | '777777'OLL
Unsigned 64-bit | "nULL | "12ULL
Long | 'n'OULL | '777777'OULL

Digits in hexadecimal constants include the letters A through F for the decimal numbers 10 through 15. Octal constant use the same style as hexadecimal constants, substituting an O for the X. Absolute values of integer constants are given in the following table.

Type | Absolute Value Range
Byte | 0 – 255
Integer | 0 – 32767
Unsigned Integer | 0 – 65535
Long | 0 – 231 - 1
Unsigned Long | 0 – 232 - 1
64-bit Long | 0 – 263 - 1
Unsigned 64-bit Long | 0 – 264 - 1

Integers specified without one of the B, S, L, or LL specifiers are automatically promoted to an integer type capable of holding them. For example, 40000 is promoted to longword because it is too large to fit in an integer. Any numeric constant can be preceded by a plus (`+`) or minus (`-`) sign. The following table illustrates examples of both valid and invalid IDL constants.

Unacceptable | Reason | Acceptable
256B | Too large, limit is 255 | 255B
'123L | Missing apostrophe | '123'L
'03G'x | Invalid character | "129
'27'L | No radix | '27'OL
650XL | No apostrophes | '650'XL
"129 | 9 is an invalid octal digit | "124

## Floating-Point and Double-Precision Constants

Floating-point and double-precision constants can be expressed in either conventional or scientific notation. Any numeric constant that includes a decimal point is a floating-point or double-precision constant.

The syntax of floating-point and double-precision constants is shown in the following table. The notation “sx” represents the sign and magnitude of the exponent, for example, `E-2`.

Form | Example
n. | 102.
.n | .102
n.n | 10.2
n `E` | 10E
n `E` sx | 10E5
n.`E` sx | 10.E-3
.n `E` sx | .1E+12
n.n `E` sx | 2.3E12

Double-precision constants are entered in the same manner, replacing the `E` with a `D`. For example, `1.0D0`, `1D`, and `1.D` each represent a double-precision numeral 1.

Note: The `nE` and `nD` forms are shorthand for `nE0` and `nD0`, and are usually used to indicate the type of the number, either single or double precision. When using these forms in expressions, be sure to leave a space after the `E` or `D` if the next term has a + or - sign.
For example, the expression `1D+45` is evaluated as 1x1045 in double precision, while `1D + 45` (note the spaces) evaluates to the number 46 in double precision. Similarly, the expression `1D+x` gives an error, because there was no space after the `D`. The correct way to write this expression is `1D + x` (note the spaces).

## Complex Constants

Complex constants contain a real and an imaginary part, both of which are single- or double-precision floating-point numbers. The imaginary part can be omitted, in which case it is assumed to be zero. The form of a complex constant is as follows:

COMPLEX(REAL_PART, IMAGINARY_PART)

or

COMPLEX(REAL_PART)

For example, COMPLEX(1,2) is a complex constant with a real part of one, and an imaginary part of two. COMPLEX(1) is a complex constant with a real part of one and a zero imaginary component. To extract the real part of a complex expression, use the FLOAT function. The ABS function returns the magnitude of a complex expression, and the IMAGINARY function returns the imaginary part.

## String Constants

A string constant consists of zero or more characters enclosed by apostrophes (`'`) or quotes (`“`). The value of the constant is simply the characters appearing between the leading delimiter (`'`or `“”`) and the next occurrence of the same delimiter. A double apostrophe (`''`) or quote (`“”`) is considered to be an empty string; a string containing no characters. An apostrophe or quote can be represented within a string by two apostrophes or quotes; e.g., `'Don''t'` returns `Don't`. This syntax often can be avoided by using a different delimiter; e.g., `“Don't”` instead of `'Don''t'`. The following table illustrates valid string constants.

Expression | Resulting String
'Hi there' | Hi there
"Hi there" | Hi there
' ' | Empty String
"I'm happy" | I’m happy
'I"m happy' | I”m happy
'counter' | counter
'129' | 129

The following table illustrates invalid string constants. In the last entry of the table, "129" is interpreted as an illegal octal constant. This is because a quote character followed by a digit from 0 to 7 represents an octal numeric constant, not a string, and the character 9 is an illegal octal digit.

String Value | Unacceptable | Reason
Hi there | 'Hi there" | Mismatched delimiters
Empty String | ' | Missing delimiter
I’m happy | 'I'm happy' | Apostrophe in string
counter | ''counter'' | Double apostrophe is an empty string
129 | "129" | Illegal octal constant

While an IDL string variable can hold up to 64 Kbytes of information, the buffer than handles input at the IDL command prompt is limited to 255 characters. If for some reason you need to create a string variable longer than 255 characters at the IDL command prompt, split the variable into multiple sub-variables and combine them with the “+” operator:

var = var1+var2+var3

This limit only affects string constants created at the IDL command prompt.

Note: See String Operations for details on working with strings.