← 🔢 Types, Operators & Expressions
Subtopic 1 of 5

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

  1. Find the size in bits (char 8, short 16, int 32, long 64).
  2. Signed range: −2n−1 to 2n−1−1. Unsigned range: 0 to 2n−1.
  3. If the true result is outside the range, take it modulo 2n (unsigned: always defined; signed: undefined in the standard, wraps in practice).
  4. 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.
int: smallest .. largest-2147483648 .. 2147483647
x stored in a int2147483647
x in binary0111 1111 1111 1111 1111 1111 1111 1111
x + y computed in int (32 bits)-2147483648
stored back into a int-2147483648
true mathematical result2147483648

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.

TypeBytesSmallestLargest
char1−128127
unsigned char10255
short2−32 76832 767
unsigned short2065 535
int4−2 147 483 6482 147 483 647
unsigned int404 294 967 295
long8−9.22 × 10¹⁸9.22 × 10¹⁸
unsigned long801.84 × 10¹⁹
float / double4 / 8≈ ±3.4 × 10³⁸ / ±1.8 × 10³⁰⁸about 7 / 15 decimal digits
any pointer8an addresssizeof(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?

Remember: n bits: wrap modulo 2n; unsigned is always defined, signed overflow is undefined.
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.