Introduction & Syntax
Data Types · Study material
Data types in C decide how much memory a value takes, which values it can hold and what operations mean. Choosing the wrong one gives some of C's subtlest bugs: -1 < 1u is false, 200 + 100 stored in an unsigned char is 44, and signed overflow is undefined behaviour. This page explains the types, their sizes and ranges, how C converts between them, and what C23 adds – then gives you 40 practice questions checked on a real compiler.
On this page
- The basic types, their sizes on 64-bit systems and their ranges
- Signed vs unsigned, wrap-around and overflow
- Integer promotion and the usual arithmetic conversions
<stdint.h>, printf formats, and C23'sbool,_BitIntand checked arithmetic
The basic types
C guarantees only minimum sizes (char ≥ 8 bits, short and int ≥ 16, long ≥ 32, long long ≥ 64). On 64-bit Linux and macOS (the "LP64" model) the sizes are:
#include <stdio.h>
int main(void) {
printf("char %zu, short %zu, int %zu, long %zu, long long %zu\n",
sizeof(char), sizeof(short), sizeof(int), sizeof(long), sizeof(long long));
printf("float %zu, double %zu, pointer %zu\n", sizeof(float), sizeof(double), sizeof(void *));
return 0;
}
char 1, short 2, int 4, long 8, long long 8 float 4, double 8, pointer 8
| Type | Size (LP64) | Typical range | printf |
|---|---|---|---|
char / signed char / unsigned char | 1 | −128…127 or 0…255 | %c / %d / %u |
short | 2 | −32 768…32 767 | %hd |
int | 4 | −2 147 483 648…2 147 483 647 | %d |
long | 8 (4 on Windows) | ±9.2 × 10¹⁸ | %ld |
long long | 8 | ±9.2 × 10¹⁸ | %lld |
float / double | 4 / 8 | ~7 / ~15 significant digits | %f |
size_t | 8 | 0…18 446 744 073 709 551 615 | %zu |
Windows uses "LLP64": long stays 4 bytes even on 64-bit. Code that needs an exact size should use the types from <stdint.h>.
Ranges from limits.h
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
int main(void) {
printf("INT_MIN = %d\nINT_MAX = %d\n", INT_MIN, INT_MAX);
printf("UINT_MAX = %u\n", UINT_MAX);
printf("LLONG_MAX = %lld\n", LLONG_MAX);
printf("SIZE_MAX = %zu\n", SIZE_MAX);
return 0;
}
INT_MIN = -2147483648 INT_MAX = 2147483647 UINT_MAX = 4294967295 LLONG_MAX = 9223372036854775807 SIZE_MAX = 18446744073709551615
Signed, unsigned and overflow
Unsigned arithmetic is defined to wrap around modulo 2ⁿ: 0 − 1 is UINT_MAX. Signed overflow is undefined behaviour – INT_MAX + 1 may give a negative number, or the optimiser may assume it never happens. Whether plain char is signed is up to the platform (signed on x86 and Apple, unsigned on most ARM Linux).
Before any arithmetic, types smaller than int (char, short) are promoted to int – so adding two unsigned chars cannot wrap, but storing the result back into one can.
#include <stdio.h>
int main(void) {
unsigned char c = 250;
c += 10; /* unsigned arithmetic wraps: (250 + 10) mod 256 */
unsigned u = 0;
u--; /* wraps to UINT_MAX */
unsigned char x = 200, y = 100;
int sum = x + y; /* both are promoted to int first: no wrap */
printf("%u %u %d\n", c, u, sum);
return 0;
}
4 4294967295 300
The usual arithmetic conversions
When an operator mixes types, both operands are converted to a common type first: to the floating type if either is floating, otherwise to the larger integer type – and if a signed and an unsigned type of the same rank meet, the signed one becomes unsigned. That is why comparing an int with an unsigned or a size_t goes wrong for negative numbers (compilers warn with -Wsign-compare).
#include <stdio.h>
int main(void) {
int a = -1;
unsigned b = 1;
/* In a < b the int is converted to unsigned: -1 becomes 4294967295 */
printf("(unsigned) a = %u\n", (unsigned) a);
printf("as unsigned: %s\n", (unsigned) a < b ? "a < b" : "a >= b");
printf("as long long: %s\n", (long long) a < (long long) b ? "a < b" : "a >= b");
return 0;
}
(unsigned) a = 4294967295 as unsigned: a >= b as long long: a < b
Fixed-width types: stdint.h
int8_t, int16_t, int32_t, int64_t and their uint… versions have exactly that many bits. Print them with the macros from <inttypes.h>, such as PRId32 and PRIu64, which expand to the right format on every platform.
#include <inttypes.h>
#include <stdio.h>
int main(void) {
int32_t a = -42; /* exactly 32 bits */
uint64_t big = UINT64_C(1) << 40; /* exactly 64 bits, unsigned */
int8_t small = INT8_MIN; /* -128 */
printf("%" PRId32 " %" PRIu64 " %" PRId8 "\n", a, big, small);
return 0;
}
-42 1099511627776 -128
What C23 adds
bool, true and false are now keywords (no <stdbool.h> needed, and sizeof(true) is 1). _BitInt(N) gives integers of exactly N bits. <stdckdint.h> adds ckd_add, ckd_sub and ckd_mul, which report overflow instead of causing undefined behaviour. C23 also requires two's complement for signed integers.
#include <limits.h>
#include <stdckdint.h>
#include <stdio.h>
int main(void) {
bool ok = true; /* C23: bool, true and false are keywords */
unsigned _BitInt(12) reading = 4095; /* C23: an unsigned integer of exactly 12 bits */
reading += 1; /* wraps to 0, like any unsigned type */
int r;
bool overflow = ckd_add(&r, INT_MAX, 1); /* C23: checked addition, no undefined behaviour */
printf("%d %d %zu %d\n", ok, (int) reading, sizeof(true), overflow);
return 0;
}
1 0 1 1
Key points to remember
sizeof(char)is 1 by definition; other sizes vary – usesizeofand<stdint.h>.- Unsigned overflow wraps; signed overflow is undefined.
- char and short are promoted to int in expressions.
- Mixing signed and unsigned converts the signed value to unsigned.
- Match printf formats to types:
%zufor size_t,%lldfor long long,PRIu64for uint64_t.
Frequently asked questions
What is the size of int in C?
At least 16 bits by the standard; 4 bytes (32 bits) on every mainstream desktop and mobile platform today.
When should I use unsigned types?
For bit patterns, sizes and counts that can never be negative (size_t), and when you need defined wrap-around. Avoid them for ordinary arithmetic that may go below zero.
Is char signed or unsigned?
It is implementation-defined. Use signed char or unsigned char (or int8_t/uint8_t) when it matters.
Where can I learn data types step by step?
Read Data Types in C, Type Conversion in C and printf and scanf in C, then practise with the exercises below.