Thursday, November 19, 2009

C Literal Constants

C Literal Constants
Site Logo
Literal constants (often referred to as literals or constants) are explicit invariant values contained with source programs.
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-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.

No comments: