Go to the previous, next section.

CDL Notation for Data Constants

This section explains the CDL notation for netCDF constants.

Attributes are initialized in the variables section of a CDL description by providing a list of constants that determines the attribute's type and length. (In the C and FORTRAN procedural interfaces to the netCDF library, the type and length of an attribute must be explicitly provided when it is defined.) Since neither C nor FORTRAN provide suitable standard syntax to distinguish between constants of type byte and char, short and long, or float and double (except that FORTRAN provides the latter), CDL defines a syntax for constant values that allows it to determine the netCDF type of any constant. The syntax for CDL constants is similar to C syntax, except that type suffixes are appended to shorts and floats to distinguish them from longs and doubles.

A byte constant is represented by a single character or multiple character escape sequence enclosed in single quotes. For example:

'a'     // ASCII a
'\0'    // a zero byte
'\n'    // ASCII newline character
'\33'   // ASCII escape character (33 octal)
'\x2b'  // ASCII plus (2b hex)
'\377'  // 377 octal = 255 decimal, a non-ASCII byte

Character constants are enclosed in double quotes. A character array may be represented as a string enclosed in double quotes. Multiple strings are concatenated into a single array of characters, permitting long character arrays to appear on multiple lines. To support multiple variable-length string values, a conventional delimiter such as `,' may be used, but interpretation of any such convention for a string delimiter must be implemented in software above the netCDF library layer. The usual escape conventions for C strings are honored. For example:

"a"            // ASCII `a'
"Two\nlines\n" // a 10-character string with two embedded newlines
"a bell:\007"  // a string containing an ASCII bell
"ab","cde"     // the same as "abcde"

The form of a short constant is an integer constant with an `s' or `S' appended. If a short constant begins with `0', it is interpreted as octal. When it begins with 0x, it is interpreted as a hexadecimal constant. For example:

2s      // a short 2
0123s   // octal
0x7ffs  // hexadecimal

The form of a long constant is an ordinary integer constant, although it is acceptable to append an optional `l' or `L'. If a long constant begins with `0', it is interpreted as octal. When it begins with 0x, it is interpreted as a hexadecimal constant. Examples of valid long constants include:

-2
1234567890L
0123            // octal
0x7ff           // hexadecimal

The float type is appropriate for representing data with about seven significant digits of precision. The form of a float constant is the same as a C floating-point constant with an `f' or `F' appended. A decimal point is required in a CDL float to distinguish it from an integer. For example, the following are all acceptable float constants:

-2.0f
3.14159265358979f       // will be truncated to less precision
1.f
.1f

The double type is appropriate for representing floating-point data with about 16 significant digits of precision. The form of a double constant is the same as a C floating-point constant. An optional `d' or `D' may be appended. A decimal point is required in a CDL double to distinguish it from an integer. For example, the following are all acceptable double constants:

-2.0
3.141592653589793
1.0e-20
1.d

Go to the previous, next section.