1.1Integer types, ranges and overflow
The idea
Every integer type is a fixed number of bits. When a value does not fit, it does not grow: it wraps around, like a car odometer going from 999999 back to 000000.
Think of it like this: A 3-digit odometer: after 999 comes 000. An 8-bit unsigned counter does the same after 255.
Why it exists
Output questions hide overflow, unsigned wrap-around and range limits inside innocent-looking arithmetic.
How it works
- Find the size in bits (char 8, short 16, int 32, long 64).
- Signed range: −2n−1 to 2n−1−1. Unsigned range: 0 to 2n−1.
- If the true result is outside the range, take it modulo 2n (unsigned: always defined; signed: undefined in the standard, wraps in practice).
- char and short are promoted to int before arithmetic.
Worked example: x = 255 in an unsigned char
255 + 1 = 256, which does not fit in 8 bits. 256 mod 256 = 0. As a signed char, 127 + 1 would become −128.
Now play with it
- →Choose unsigned char, x = 255, y = 1, operator +, and see the result computed as an int (256) but stored as 0.
- →Switch the type to int with x = 2147483647 and watch the sum wrap to a negative number.
Red means the value did not fit and wrapped around (modulo 232). For signed types real C calls this undefined behaviour; on common machines it wraps like this. Types smaller than int are promoted to int before the operation.
| Type | Bytes | Smallest | Largest |
|---|---|---|---|
| char | 1 | −128 | 127 |
| unsigned char | 1 | 0 | 255 |
| short | 2 | −32 768 | 32 767 |
| unsigned short | 2 | 0 | 65 535 |
| int | 4 | −2 147 483 648 | 2 147 483 647 |
| unsigned int | 4 | 0 | 4 294 967 295 |
| long | 8 | −9.22 × 10¹⁸ | 9.22 × 10¹⁸ |
| unsigned long | 8 | 0 | 1.84 × 10¹⁹ |
| float / double | 4 / 8 | ≈ ±3.4 × 10³⁸ / ±1.8 × 10³⁰⁸ | about 7 / 15 decimal digits |
| any pointer | 8 | an address | sizeof(int*) = sizeof(char*) = 8 |
Sizes shown are for a typical 64-bit compiler. GATE questions state the sizes they assume, or use sizeof-independent reasoning.
Check yourself
What is the value of an unsigned int u = 0 after u = u − 1?
Exam notes (the dense version)
On a typical machine char = 1, short = 2, int = 4, long = 8 bytes. An n-bit signed type holds −2n−1 … 2n−1−1, an unsigned one 0 … 2n−1. Unsigned arithmetic wraps modulo 2n; signed overflow is undefined behaviour (in practice it wraps, but the exam may call it undefined).
Previous year questions: Types, Operators & Expressions (2)
PYQs are grouped by topic, so this is the full set for Types, Operators & Expressions; try the ones that match this subtopic. Open all 2 on the topic page.