Octal constants consist of digits 0 to 7, and begin with
0. Decimal constants consist of
digits 0 to 9, but must not start with 0, or they will be mistaken for octals.
Hexadecimal constants start with 0x or 0X, and use the
digits 0 to 9, plus A to
F and a to
f. Integer constants are always
non-negative, and do not include a sign.
- constant
integer-constant- integer-constant
untyped-integer-constant integer-suffixopt- untyped-integer-constant
decimal-constantoctal-constanthexadecimal-constant- decimal-constant
nonzero-digitdecimal-constant digit- octal-constant
0octal-constant octal-digit- hexadecimal-constant
hexadecimal-prefix hexadecimal-digithexadecimal-constant hexadecimal-digit- hexadecimal-prefix
0x0X- digit
-
any of
0 1 2 3 4 5 6 7 8 9 - nonzero-digit
-
any of
1 2 3 4 5 6 7 8 9 - octal-digit
-
any of
0 1 2 3 4 5 6 7 - hexadecimal-digit
-
any of
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
The integer suffix indicates the precise type of the
constant. L indicates long or
unsigned long.
LL indicates long long or
unsigned long long.
U indicates unsigned,
unsigned long or
unsigned long long.
- integer-suffix
unsigned-suffix long-suffixoptunsigned-suffix long-long-suffixlong-suffix unsigned-suffixoptlong-long-suffix unsigned-suffixopt- unsigned-suffix
uU- long-suffix
lL- long-long-suffix
llLL
There are no constants for char,
unsigned char,
signed char,
short or
unsigned short. Just
use a constant for the other types, and let it be
truncated.
There is no built-in notation for integer constants expressed in binary. However, you can use macros to build one:
#include<stdio.h>#include<inttypes.h>// Mathew Hendry's macro for binary integer literals - C / C++ #define BIT(N, K) (((N >>(4*K)) & 1) << K) #define HCB(N) \ (BIT(N, 0) | BIT(N, 1) | BIT(N, 2) | BIT(N, 3) | \ BIT(N, 4) | BIT(N, 5) | BIT(N, 6) | BIT(N, 7)) #define BIN8(S) (HCB(0x ## S ## UL)) #define BIN32(A, B, C, D) \ ((HCB(0x ## A ## UL) << 24) | (HCB(0x ## B ## UL) << 16) | (HCB(0x ## C ## UL) << 8) | (HCB(0x ## D ## UL))) #define TEST8(S) printf("%8s -> %016jX\n", #S, (uintmax_t) BIN8(S)) #define TEST32(A,B,C,D) \ printf("%8s%8s%8s%8s -> %016jX\n", #A, #B, #C, #D, \ (uintmax_t) BIN32(A, B, C, D)) int main() { TEST8(00100110); TEST32(1001,11,1100011,11011011); return 0; }
00100110 -> 0000000000000026
1001 11 110001111011011 -> 00000000090363DB