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-constant
octal-constant
hexadecimal-constant
- decimal-constant
nonzero-digit
decimal-constant digit
- octal-constant
0
octal-constant octal-digit
- hexadecimal-constant
hexadecimal-prefix hexadecimal-digit
hexadecimal-constant hexadecimal-digit
- hexadecimal-prefix
0x
0X
- 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-suffixopt
unsigned-suffix long-long-suffix
long-suffix unsigned-suffixopt
long-long-suffix unsigned-suffixopt
- unsigned-suffix
u
U
- long-suffix
l
L
- long-long-suffix
ll
LL
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