>  Docs Center  >  Using IDL  >  Defining and Using Constants

Defining and Using Constants

Defining and Using Constants

IDL can create a variety of numeric and string constants, with the data type and value depending upon the syntax.

When creating constants in your programs, you should know the data type of the constant and how it will be used in expressions. Having an incorrect data type for a constant can be inefficient and change your results.

For example, if the data type does not match the other types in an expression, then either the other variables or your constant will be converted to match. Consider the following expression:

A = FINDGEN(100)
B = 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. In this case it would be better to write:

A = FINDGEN(100)
B = A + 5.0

On the other hand, imagine A is of type byte:

A = BINDGEN(100)
B = A + 5

In this case the computation will be done using integer arithmetic and the result will be an array of type integer, leading to unexpected results if your algorithm expected variable B to be type byte. Here, it would be better to write:

A = BINDGEN(100)
B = A + 5b

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 are represented by the constant followed by an optional suffix specifying the type:




Byte 8-bit unsigned


12b, 34B, 34ub

Short 16-bit integer

(nothing), S

12, 12s, 425S

Unsigned short integer


12u, 12uS

Long 32-bit integer


12l, 94L

Unsigned long integer


12uL, 94UL

64-bit integer


12ll, 94LL

Unsigned 64-bit integer


12ull, 94uLL

Note: The suffix is case insensitive.

The syntax used when creating integer constants is shown in the following table, where n represents one or more digits and <T> represents the optional type suffix from the table above.






12, 12b, 425ull

(digits 0–9, A–F)

0xn<T> 0x3A, 0x3AuLL


'3A'x, '3A'xull

(digits 0–7)
'n'o<T> '377'o, '777777'oUL
"n<T> "12, "12ub

Note: For the Octal form with the double-quote character, be careful that you do not have another double quote at the end of your constant. Otherwise it will be interpreted as a string. It is strongly recommended that you use the 'n'o syntax instead.

Note: To create a hexadecimal constant of type byte, be sure to use ub for the suffix, not b. Otherwise the b may be treated as part of your hex number. For example, a = 0x5Bub.

Absolute values of integer constants are given in the following table.


Absolute Value Range


0 – 255


0 – 32767

Unsigned Integer

0 – 65535


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.





Too large, limit is 255



Missing apostrophe



Invalid character



No radix



9 is an invalid octal digit


Short Integers and the DEFINT32 Compile Option

When creating short 16-bit integers (signed or unsigned), you can omit the "S" if the DEFINT32 (or IDL2) compile option is not turned on. If the DEFINT32 (or IDL2) compile option is turned on, then by default any integer constants will be 32-bits and you need to use the "S" to force IDL to create 16-bit integers. For example:

IDL> help, 1234, 1234u
  <Expression>    INT       =     1234
  <Expression>    UINT      =     1234
IDL> compile_opt DEFINT32
IDL> help, 1234, 1234u
  <Expression>    LONG      =         1234
  <Expression>    ULONG     =         1234
IDL> help, 1234s, 1234us
  <Expression>    INT      =         1234
  <Expression>    UINT     =         1234

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.



















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:




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 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.


Resulting String

'Hi there'

Hi there

"Hi there"

Hi there


Empty String

"I'm happy"

I'm happy

'I''m happy'

I'm happy





The following table illustrates invalid string constants.




'Hi there"

Mismatched delimiters

"Hi there"


Missing delimiter

'' or ""

'I'm happy'

Apostrophe in string

'I''m happy' or "I'm happy"


Missing quote, will be treated as octal


Note: String constants that start with a double-quote character and a number may be incorrectly interpreted as an octal constant. If the number is followed by an operator (+-*/^?:,)) then this will always be treated as an octal constant, even if there is a trailing double-quote character. To avoid ambiguity, you should always use single-quote characters with strings that begin with a number.

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

© 2020 Harris Geospatial Solutions, Inc. |  Legal
My Account    |    Store    |    Contact Us