Allows values of integer type to be used in expressions directly.
An integer constant is a non-lvalue expression of the form.
decimal-constant integer-suffix(optional) | (1) | |
octal-constant integer-suffix(optional) | (2) | |
hex-constant integer-suffix(optional) | (3) |
where.
1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
), followed by zero or more decimal digits (0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
) 0
) followed by zero or more octal digits (0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
) 0x
or the character sequence 0X
followed by one or more hexadecimal digits (0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
, a
, A
, b
, B
, c
, C
, d
, D
, e
, E
, f
, F
) u
or the character U
) l
or the character L
) or the long-long-suffix (the character sequence ll
or the character sequence LL
) (since C99) The following variables are initialized to the same value:
int d = 42; int o = 052; int x = 0x2a; int X = 0X2A;
The type of the integer constant is the first type in which the value can fit, from the list of types which depends on which numeric base and which integer-suffix was used.
Types allowed for integer constants | ||
---|---|---|
suffix | decimal bases | hexadecimal or octal bases |
no suffix | int
| int
|
u or U | unsigned int
| unsigned int
|
l or L | long int
| long int
|
both l /L and u /U | unsigned long int
| unsigned long int
|
ll or LL | long long int (since C99) | long long int (since C99)
|
both ll /LL and u /U | unsigned long long int (since C99) | unsigned long long int (since C99) |
If the value of the integer constant is too big to fit in any of the types allowed by suffix/base combination and the compiler supports extended integer types (such as __int128
), the constant may be given the extended integer type; otherwise, the program is ill-formed.
Letters in the integer constants are case-insensitive: 0xDeAdBaBeU
and 0XdeadBABEu
represent the same number (one exception is the long-long-suffix, which is either ll
or LL
, never lL
or Ll
).
There are no negative integer constants. Expressions such as -1
apply the unary minus operator to the value represented by the constant, which may involve implicit type conversions.
When used in a controlling expression of #if or #elif, all signed integer constants act as if they have type intmax_t
and all unsigned integer constants act as if they have type uintmax_t
.
Integer constants may be used in integer constant expressions.
#include <stdio.h> int main(void) { printf("123 = %d\n", 123); printf("0123 = %d\n", 0123); printf("0x123 = %d\n", 0x123); printf("12345678901234567890ull = %llu\n", 12345678901234567890ull); // the type is unsigned long long even without a long long suffix printf("12345678901234567890u = %llu\n", 12345678901234567890u ); // printf("%lld\n", -9223372036854775808); // ERROR // the value 9223372036854775808 cannot fit in signed long long, which is the // biggest type allowed for unsuffixed decimal integer constant printf("%llu\n", -9223372036854775808u ); // unary minus applied to unsigned value subtracts it from 2^64, // this gives 9223372036854775808 printf("%lld\n", -9223372036854775807 - 1); // // correct way to represent the value -9223372036854775808 }
Output:
123 = 123 0123 = 83 0x123 = 291 12345678901234567890ull = 12345678901234567890 12345678901234567890u = 12345678901234567890 9223372036854775808 -9223372036854775808
C++ documentation for integer literal |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/language/integer_constant