Literals

The term literal constant, or literal, refers to a value that occurs in a program and cannot be changed. The C language uses the term constant in place of the noun literal. The adjective literal adds to the concept of a constant the notion that we can speak of it only in terms of its value. A literal constant is nonaddressable, which means that its value is stored somewhere in memory, but we have no means of accessing that address.

Every literal has a value and a data type. The value of any literal does not change while the program runs and must be in the range of representable values for its type. The following are the available types of literals:

Integer literals

Integer literals are numbers that do not have a decimal point or an exponential part. They can be represented as:

An integer literal may have a prefix that specifies its base, or a suffix that specifies its type.

Read syntax diagramSkip visual syntax diagramInteger literal syntax
 
>>-+-decimal_constant-----+--+---------------+-----------------><
   +-octal_constant-------+  +-+-l--+--+---+-+
   '-hexadecimal_constant-'  | +-L--+  +-u-+ |
                             | +-ll-+  '-U-' |
                             | '-LL-'        |
                             '-+-u-+--+----+-'
                               '-U-'  +-l--+
                                      +-L--+
                                      +-ll-+
                                      '-LL-'
 

The data type of an integer literal is determined by its form, value, and suffix. The following table lists the integer literals and shows the possible data types. The smallest data type that can represent the constant value is used to store the constant.

Integer literal Possible data types
unsuffixed decimal int, long int, C only long long int1
unsuffixed octal or hexadecimal int, unsigned int, long int, unsigned long int, C only long long int1, C only unsigned long long int1
decimal, octal, or hexadecimal suffixed by u or U unsigned int, unsigned long int, C only unsigned long long int1
decimal suffixed by l or L long int, C only long long int1
octal or hexadecimal suffixed by l or L long int, unsigned long int, C only long long int1, C only unsigned long long int1
decimal, octal, or hexadecimal suffixed by both u or U, and l or L unsigned long int, C only unsigned long long int1
decimal suffixed by ll or LL long long int
octal or hexadecimal suffixed by ll or LL long long int, unsigned long long int
decimal, octal, or hexadecimal suffixed by both u or U, and ll or LL unsigned long long int
Notes:
  1. Requires compilation with c99 or with -qlanglvl=stdc99 or -qlanglvl=extc99 or the equivalent pragmas.

Related information

Decimal integer literals

A decimal integer literal contains any of the digits 0 through 9. The first digit cannot be 0. Integer literals beginning with the digit 0 are interpreted as an octal integer literal rather than as a decimal integer literal.

Read syntax diagramSkip visual syntax diagramDecimal integer literal syntax
 
                 .--------------.
                 V              |
>>-digit_1_to_9----digit_0_to_9-+------------------------------><
 

A plus (+) or minus (-) symbol can precede a decimal integer literal. The operator is treated as a unary operator rather than as part of the literal.

The following are examples of decimal literals:

485976
-433132211
+20
5
Hexadecimal integer literals

A hexadecimal integer literal begins with the 0 digit followed by either an x or X, followed by any combination of the digits 0 through 9 and the letters a through f or A through F. The letters A (or a) through F (or f) represent the values 10 through 15, respectively.

Read syntax diagramSkip visual syntax diagramHexadecimal integer literal syntax
 
           .------------------.
           V                  |
>>-+-0x-+----+-digit_0_to_f-+-+--------------------------------><
   '-0X-'    '-digit_0_to_F-'
 

The following are examples of hexadecimal integer literals:

0x3b24 
0XF96 
0x21 
0x3AA 
0X29b 
0X4bD
Octal integer literals

An octal integer literal begins with the digit 0 and contains any of the digits 0 through 7.

Read syntax diagramSkip visual syntax diagramOctal integer literal syntax
 
      .--------------.
      V              |
>>-0----digit_0_to_7-+-----------------------------------------><
 

The following are examples of octal integer literals:

0 
0125 
034673 
03245

Boolean literals

C only At the C99 level, C defines true and false as macros in the header file stdbool.h.

C++ There are only two Boolean literals: true and false.

Related information

Floating-point literals

Floating-point literals are numbers that have a decimal point or an exponential part. They can be represented as:

Decimal floating-point literals

A real decimal floating-point constant consists of the following:

Both the integral and fractional parts are made up of decimal digits. You can omit either the integral part or the fractional part, but not both. You can omit either the decimal point or the exponent part, but not both.

Read syntax diagramSkip visual syntax diagramDecimal floating-point literal syntax
 
     .-----------.     .-------.
     V           |     V       |
>>-+---+-------+-+--.----digit-+--+--------------+-+--+---+----><
   |   '-digit-'                  '-| exponent |-' |  +-f-+
   | .-------.                                     |  +-F-+
   | V       |                                     |  +-l-+
   +---digit-+--.--+--------------+----------------+  '-L-'
   |               '-| exponent |-'                |
   | .-------.                                     |
   | V       |                                     |
   '---digit-+--| exponent |-----------------------'
 
Exponent:
 
                  .-------.
                  V       |
|--+-e-+--+----+----digit-+-------------------------------------|
   '-E-'  +-+--+
          '- --'
 

The suffix f or F indicates a type of float, and the suffix l or L indicates a type of long double. If a suffix is not specified, the floating-point constant has a type double.

A plus (+) or minus (-) symbol can precede a floating-point literal. However, it is not part of the literal; it is interpreted as a unary operator.

The following are examples of decimal floating-point literals:

Floating-point constant Value
5.3876e4 53,876
4e-11 0.00000000004
1e+5 100000
7.321E-3 0.007321
3.2E+4 32000
0.5e-6 0.0000005
0.45 0.45
6.e10 60000000000

Related information

Hexadecimal floating-point literals

Real hexadecimal floating constants, which are a C99 feature, consist of the following:

The significant part represents a rational number and is composed of the following:

The optional fraction part is a period followed by a sequence of hexadecimal digits.

The exponent part indicates the power of 2 to which the significant part is raised, and is an optionally signed decimal integer. The type suffix is optional. The full syntax is as follows:

Read syntax diagramSkip visual syntax diagramHexadecimal floating-point literal syntax
 
>>-+-0x-+------------------------------------------------------->
   '-0X-'
 
     .------------------.     .------------------.
     V                  |     V                  |
>--+---+--------------+-+--.----+-digit_0_to_f-+-+--| exponent |-+-->
   |   +-digit_0_to_f-+         '-digit_0_to_F-'                 |
   |   '-digit_0_to_F-'                                          |
   | .------------------.                                        |
   | V                  |                                        |
   +---+-digit_0_to_f-+-+--.--| exponent |-----------------------+
   |   '-digit_0_to_F-'                                          |
   | .------------------.                                        |
   | V                  |                                        |
   '---+-digit_0_to_f-+-+--| exponent |--------------------------'
       '-digit_0_to_F-'
 
>--+---+-------------------------------------------------------><
   +-f-+
   +-F-+
   +-l-+
   '-L-'
 
Exponent:
 
                  .--------------.
                  V              |
|--+-p-+--+----+----digit_0_to_9-+------------------------------|
   '-P-'  +-+--+
          '- --'
 

The suffix f or F indicates a type of float, and the suffix l or L indicates a type of long double. If a suffix is not specified, the floating-point constant has a type double. You can omit either the whole-number part or the fraction part, but not both. The binary exponent part is required to avoid the ambiguity of the type suffix F being mistaken for a hexadecimal digit.

Complex literals

Complex literals, which are a C99 feature, are constructed in two parts: the real part, and the imaginary part.

Read syntax diagramSkip visual syntax diagramComplex literal syntax
 
>>-| real part |--+------+--| imaginary part |------------------>
                  '-+----'
 
>--| Imaginary part: |------------------------------------------>
 
>--floating-point constant--*--_Complex_I----------------------><
 
Real part:
 
|--floating-point constant--------------------------------------|
 

The floating-point constant can be specified as a decimal or hexadecimal floating-point constant (including optional suffixes), in any of the formats described in the previous sections.

_Complex_I is a macro defined in the complex.h header file, representing the imaginary unit i, the square root of -1.

For example, the declaration:

varComplex = 2.0f + 2.0f * _Complex_I;

initializes the complex variable varComplex to a value of 2.0 + 2.0i.

IBM extension

For ease of porting applications developed with GNU C, XL C/C++ also allows you to indicate the imaginary part of a complex literal with a suffix, in addition to the standard suffixes that indicate the type of the complex number (float, double, or long double).

The simplified syntax for a complex literal using the GNU suffixes is as follows:

Read syntax diagramSkip visual syntax diagram>>-| real part |--+------+--| imaginary part |-----------------><
                  '-+----'
 
real part:
 
|----floating-point constant------------------------------------|
 
imaginary part:
 
|--floating-point constant--imaginary-suffix--------------------|
 

floating-point constant can be specified as a decimal or hexadecimal floating-point constant (including optional suffixes), in any of the formats described in the previous sections.

imaginary-suffix is one of the suffixes i, I, j, or J, representing the imaginary unit.

For example, the declaration

varComplex = 3.0f + 4.0fi;

initializes the complex variable varComplex to a value of 3.0 + 4.0i.

End of IBM extension

Related information

IBM extension

Vector literals

A vector literal is a constant expression for which the value is interpreted as a vector type. The data type of a vector literal is represented by a parenthesized vector type, and its value is represented by a parenthesized set of constant expressions that represent the vector elements. When all vector elements have the same value, the value of the literal can be represented by a single parenthesized constant expression. Vector literals allow the initialization of vector types.

Read syntax diagramSkip visual syntax diagramVector literal syntax
 
                         .-,-----------------------.
                         V                         |
>>-(--vector_type--)--(------constant_expression---+--)--------><
 

The vector_type is a supported vector type.

The constant_expression can be either of the following:

For example, for an unsigned integer vector type, the literal could be either of the following:

(vector unsigned int)(10)   /* initializes all four elements to a value of 10  */
(vector unsigned int)(14, 82, 73, 700)    /* initializes the first element
																				 to 14, the second element to 82,
																				 the third element to 73, and the
																				 fourth element to 700   */																				

A vector literal can be cast to another vector type. A vector literal cast does not change the bit pattern of the operand: the 128 bits representing the value remains the same before and after the cast.

A full list of vector literals is provided in Appendix C. Vector data types and literals.

Related information

End of IBM extension

Character literals

A character literal contains a sequence of characters or escape sequences enclosed in single quotation mark symbols, for example 'c'. A character literal may be prefixed with the letter L, for example L'c'. A character literal without the L prefix is an ordinary character literal or a narrow character literal. A character literal with the L prefix is a wide character literal. An ordinary character literal that contains more than one character or escape sequence (excluding single quotes ('), backslashes (\) or new-line characters) is a multicharacter literal.

C The type of a narrow character literal is int. The type of a wide character literal is wchar_t. The type of a multicharacter literal is int.

C++ The type of a character literal that contains only one character is char, which is an integral type. The type of a wide character literal is wchar_t. The type of a multicharacter literal is int.

Read syntax diagramSkip visual syntax diagramCharacter literal syntax
 
             .---------------------.
             V                     |
>>-+---+--'----+-character-------+-+--'------------------------><
   '-L-'       '-escape_sequence-'
 

At least one character or escape sequence must appear in the character literal, and the character literal must appear on a single logical source line.

The characters can be from the source program character set. You can represent the double quotation mark symbol by itself, but to represent the single quotation mark symbol, you must use the backslash symbol followed by a single quotation mark symbol ( \' escape sequence). (See Escape sequences for a list of other characters that are represented by escape characters.)

Outside of the basic source character set, the universal character names for letters and digits are allowed in C++ and at the C99 language level. To enable universal character names, you must compile with the c99 invocation command, the -qlanglvl=extc99 or -qlanglvl=stdc99 options or related pragmas (for C), or the -qlanglvl=ucs (for C and C++) option.

The following are examples of character literals:

'a' 
'\'' 
L'0' 
'('

Related information

String literals

A string literal contains a sequence of characters or escape sequences enclosed in double quotation mark symbols. A string literal with the prefix L is a wide string literal. A string literal without the prefix L is an ordinary or narrow string literal.

C The type of a narrow string literal is array of char. The type of a wide string literal is array of wchar_t.

C++ The type of a narrow string literal is array of const char. The type of a wide string literal is array of const wchar_t. Both types have static storage duration.

A null ('\0') character is appended to each string. For a wide string literal, the value '\0' of type wchar_t is appended. By convention, programs recognize the end of a string by finding the null character.

Read syntax diagramSkip visual syntax diagramString literal syntax
 
             .---------------------.
             V                     |
>>-+---+--"----+-character-------+-+--"------------------------><
   '-L-'       '-escape_sequence-'
 

Multiple spaces contained within a string literal are retained.

Use the escape sequence \n to represent a new-line character as part of the string. Use the escape sequence \\ to represent a backslash character as part of the string. You can represent a single quotation mark symbol either by itself or with the escape sequence \'. You must use the escape sequence \" to represent a double quotation mark.

Outside of the basic source character set, the universal character names for letters and digits are allowed in C++ and at the C99 language level. To enable universal character names, you must compile with the c99 invocation command, the -qlanglvl=extc99 or -qlanglvl=stdc99 options or related pragmas (for C), or the -qlanglvl=ucs (for C and C++) option.

IBM extension The Pascal string form of a string literal is also accepted, provided that you compile with the -qmacpstr option.

The following are examples of string literals:

char titles[ ] = "Handel's \"Water Music\""; 
char *temp_string = "abc" "def" "ghi";  /* *temp_string = "abcdefghi\0" */ 
wchar_t *wide_string = L"longstring";

This example illustrates escape sequences in string literals:

#include <iostream> using namespace std;

int main () {
       char *s ="Hi there! \n";       
    		cout << s;
       char *p = "The backslash character \\.";
       cout << p << endl;
       char *q = "The double quotation mark \".\n";
       cout << q ;
 }  

This program produces the following output:

Hi there! The backslash character \. The double quotation mark ".

To continue a string on the next line, use the line continuation character (\ symbol) followed by optional whitespace and a new-line character (required). For example:

char *mail_addr = "Last Name    First Name    MI   Street Address \
       893    City     Province   Postal code ";

In the following example, the string literal second causes a compile-time error.

char *first = "This string continues onto the next\   
      line, where it ends.";                     /* compiles successfully.   */ 

char *second = "The comment makes the \          /* continuation symbol
      */   invisible to the compiler.";          /* compilation error.       */

Note:
When a string literal appears more than once in the program source, how that string is stored depends on whether strings are read-only or writeable. By default, the compiler considers strings to be read-only. XL C/C++ may allocate only one location for a read-only string; all occurrences will refer to that one location. However, that area of storage is potentially write-protected. If strings are writeable, then each occurrence of the string will have a separate, distinct storage location that is always modifiable. You can use the #pragma strings directive or the -qro compiler option to change the default storage for string literals.

Related information

String concatenation

Another way to continue a string is to have two or more consecutive strings. Adjacent string literals will be concatenated to produce a single string. For example:

"hello " "there"     /* is equivalent to "hello there"  */ 
"hello" "there"      /* is equivalent to "hellothere"                       */

Characters in concatenated strings remain distinct. For example, the strings "\xab" and "3" are concatenated to form "\xab3 ". However, the characters \xab and 3 remain distinct and are not merged to form the hexadecimal character \xab3 .

If a wide string literal and a narrow string literal are adjacent, as in the following:

"hello " L"there"   

the result is a wide string literal.

Following any concatenation, '\0' of type char is appended at the end of each string. For a wide string literal, '\0' of type wchar_t is appended. C++ programs find the end of a string by scanning for this value. For example:

char *first = "Hello ";            /* stored as "Hello \0"       */ 
char *second = "there";            /* stored as "there\0"        */ 
char *third = "Hello " "there";    /* stored as "Hello there\0"  */

Related information