Integer types and constants
suggest changeSigned integers can be of these types (the int
after short
, or long
is optional):
signed char c = 127; /* required to be 1 byte, see remarks for further information. */
signed short int si = 32767; /* required to be at least 16 bits. */
signed int i = 32767; /* required to be at least 16 bits */
signed long int li = 2147483647; /* required to be at least 32 bits. */
signed long long int li = 2147483647; /* required to be at least 64 bits */
Each of these signed integer types has an unsigned version.
unsigned int i = 65535;
unsigned short = 2767;
unsigned char = 255;
For all types but char
the signed
version is assumed if the signed
or unsigned
part is omitted. The type char
constitutes a third character type, different from signed char
and unsigned char
and the signedness (or not) depends on the platform.
Different types of integer constants (called literals in C jargon) can be written in different bases, and different width, based on their prefix or suffix.
/* the following variables are initialized to the same value: */
int d = 42; /* decimal constant (base10) */
int o = 052; /* octal constant (base8) */
int x = 0xaf; /* hexadecimal constants (base16) */
int X = 0XAf; /* (letters 'a' through 'f' (case insensitive) represent 10 through 15) */
Decimal constants are always signed
. Hexadecimal constants start with 0x
or 0X
and octal constants start just with a 0
. The latter two are signed
or unsigned
depending on whether the value fits into the signed type or not.
/* suffixes to describe width and signedness : */
long int i = 0x32; /* no suffix represent int, or long int */
unsigned int ui = 65535u; /* u or U represent unsigned int, or long int */
long int li = 65536l; /* l or L represent long int */
Without a suffix the constant has the first type that fits its value, that is a decimal constant that is larger than INT_MAX
is of type long
if possible, or long long
otherwise.
The header file <limits.h>
describes the limits of integers as follows. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown below, with the same sign.
Macro | Type | Value —— | —— | —— CHAR_BIT
| smallest object that is not a bit-field (byte) | 8 SCHAR_MIN
| signed char
| -127 / -(27 - 1) SCHAR_MAX
| signed char
| +127 / 27 - 1 UCHAR_MAX
| unsigned char
| 255 / 28 - 1 CHAR_MIN
| char
| see below CHAR_MAX
| char
| see below SHRT_MIN
| short int
| -32767 / -(215 - 1) SHRT_MAX
| short int
| +32767 / 215 - 1 USHRT_MAX
| unsigned short int
| 65535 / 216 - 1 INT_MIN
| int
| -32767 / -(215 - 1) INT_MAX
| int
| +32767 / 215 - 1 UINT_MAX
| unsigned int
| 65535 / 216 - 1 LONG_MIN
| long int
| -2147483647 / -(231 - 1) LONG_MAX
| long int
| +2147483647 / 231 - 1 ULONG_MAX
| unsigned long int
| 4294967295 / 232 - 1
Macro | Type | Value —— | —— | —— LLONG_MIN
| long long int
| -9223372036854775807 / -(263 - 1) LLONG_MAX
| long long int
| +9223372036854775807 / 263 - 1 ULLONG_MAX
| unsigned long long int
| 18446744073709551615 / 264 - 1
If the value of an object of type char
sign-extends when used in an expression, the value of CHAR_MIN
shall be the same as that of SCHAR_MIN
and the value of CHAR_MAX
shall be the same as that of SCHAR_MAX
. If the value of an object of type char
does not sign-extend when used in an expression, the value of CHAR_MIN
shall be 0 and the value of CHAR_MAX
shall be the same as that of UCHAR_MAX
.
The C99 standard added a new header, <stdint.h>
, which contains definitions for fixed width integers. See the fixed width integer example for a more in-depth explanation.