C++ Now

C++ Literal Constants

Site Logo

Literal constants (often referred to as literals or constants) are invariants whose values are implied by their representations.

Form

 

literal-constant  ::=  integer-literal
 |  float-literal
 |  string-literal
 |  boolean-literal
integer-literal  ::=  unsigned-literal
 |  unsigned-literal integer-suffix
 |  char-literal
unsigned-literal  ::=  decimal-literal
 |  octal-literal
 |  hex-literal
decimal-literal  ::=  nonzero-digit digit
octal-literal  ::=  0 octal-digit
hex-literal  ::=  hex-prefix hex-digit hex-digit
hex-prefix  ::=  0X
 |  0x
integer-suffix  ::=  long-suffix
 |  unsigned-suffix
 |  unsigned-suffix long-suffix
 |  long-suffix unsigned-suffix
long-suffix  ::=  L
 |  l
unsigned-suffix  ::=  U
 |  u
float-literal  ::=  unsigned-float
 |  unsigned-float float-suffix
unsigned-float  ::=  simple-float
 |  exponential-float
simple-float  ::=  digit-sequence .
 |  digit-sequence . digit-sequence
 |  . digit-sequence
exponential-float  ::=  digit-sequence exponent-part
 |  simple-float exponent-part
exponent-part  ::=  exponent-prefix exponent
exponent-prefix  ::=  E
 |  e
exponent  ::=  sign digit-sequence
 |  digit-sequence
digit-sequence  ::=  digit digit
float-suffix  ::=  L
 |  l
 |  F
 |  f
char-literal  ::=  ' c-character ∗ '
 |  L' c-character ∗ '
s-character  ::=  ASCII codes 1 to 126 except for ', \, carriage return, and line feed
 |  escape-sequence
string-literal  ::=  " s-character ∗ "
 |  L" s-character ∗ "
s-character  ::=  ASCII codes 1 to 126 except for ", \, carriage return, and line feed
 |  escape-sequence
escape-sequence  ::=  simple-escape
 |  octal-escape
 |  hex-escape
simple-escape  ::=  \a | \b | \f | \n | \r | \t | \v | \' | \" | \? | \\
octal-escape  ::=  \ octal-digit
 |  \ octal-digit octal-digit
 |  \ octal-digit octal-digit octal-digit
hex-escape  ::=  \x hex-digit
boolean-literal  ::=  true
 |  false
null-literal  ::=  null
octal-digit  ::=  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
digit  ::=  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
nonzero-digit  ::=  1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
hex-digit  ::=  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
| A | B | C | D | E | F | a | b | c | d | e | f
sign  ::=  + | -
C++ Literals
Type  Base  Example  Description 
char * ASCII "hello" Any string of characters enclosed in double quotes (") (see Note 1)
char ASCII '1' One character in single quotes (')
unsigned short int ASCII L'ab' One or two characters in single quotes ('), preceded by the letter L
int octal 01 Any octal number (digits 0-7) beginning with a 0 (zero)
decimal 1 Any decimal number (digits 0-9) not beginning with a 0 (zero)
hexadecimal 0x1 0X (zero X) or 0x (zero x) followed by any hexadecimal number (digits 0-F)
ASCII 'ABC' Two to four characters in single quotes (')
unsigned int octal 01U Any octal number (digits 0-7) beginning with a 0 (zero) and followed by U or u
decimal 1U Any decimal number (digits 0-9) not beginning with a 0 (zero) and followed by U or u
hexadecimal 0x1U 0X (zero X) or 0x (zero x) followed by any hexadecimal number (digits 0-F) and followed by U or u
long int octal 01L Any octal number (digits 0-7) beginning with a 0 (zero) and followed by L or l
decimal 1L Any decimal number (digits 0-9) not beginning with a 0 (zero) and followed by L or l
hexadecimal 0x1L 0X (zero X) or 0x (zero x) followed by any hexadecimal number (digits 0-F) and followed by L or l
unsigned long int octal 01UL Any octal number (digits 0-7) beginning with a 0 (zero) and followed by U or u and L or l
decimal 1UL Any decimal number (digits 0-9) not beginning with a 0 (zero) and followed by U or u and L or l
hexadecimal 0x1UL 0X (zero X) or 0x (zero x) followed by any hexadecimal number (digits 0-F) and followed by U or u and L or l
float decimal 12.3F Any number (digits 0-9) containing a decimal point (.) and followed by F or f
decimal 12E1F Any number (digits 0-9) followed by E or e, followed by an exponent of 10 (12E1 = 12 * 101 = 120.), and followed by F or f
double decimal 12.3 Any number (digits 0-9) containing a decimal point (.)
decimal 12E1 Any number (digits 0-9) followed by E or e and followed by an exponent of 10 (12E1 = 12 * 101 = 120.)
long double decimal 12.3L Any number (digits 0-9) containing a decimal point (.) and followed by L or l
decimal 12E1L Any number (digits 0-9) followed by E or e, followed by an exponent of 10 (12E1 = 12 * 101 = 120.), and followed by L or l
Notes:
  • String constants are stored as the literal characters followed by a byte of binary 0.  The value returned is a pointer to the first character. 
Escape Sequences
Code  Character  Description 
\\ \ Backslash
\' ' Single Quote
\" " Double Quote
\? ? Question Mark
\0 <NUL> Binary 0
\a <BEL> Bell (Audible alert)
\b <BS> Back Space
\f <FF> Form Feed
\n <NL> New Line
\r <CR> Carriage Return
\t <HT> Horizontal Tab
\v <VT> Vertical Tab
Notes:
  • These escape sequences are used within character or string literal constants.  They each represent a single character. 
Numeric Escape Sequences
Width  Code  Where 
8 bit \nnn n = octal digit
8 bit \xnn n = hexadecimal digit
8 bit \Xnn n = hexadecimal digit
16 bit \unnnn n = hexadecimal digit
32 bit \Unnnnnnnn n = hexadecimal digit
Notes:
  • These escape sequences are used within character or string literal constants.  They each represent a 1, 2, or 4 bytes, as indicated by the width. 
C++ Now Home  C++ Now Home
Etymology Index  Etymology Index
E-Mail
Valid HTML 4.01 Transitional Valid CSS! Site Logo

copyright 2000-2006, j.h.young, revised 2/6/06