Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 135 ยท Variables and Types

Integer Types

Master C integers - ranges of short, int, long and long long, exact-width types from stdint.h, printing them with inttypes.h, overflow rules and C23 literals.

Integers are the most used values in C. Loop counters, array indexes, file sizes, pixel colors, error codes and timestamps are all whole numbers, and C offers a whole family of integer types so you can pick exactly the size and signedness you need. The price of that flexibility is a few sharp rules, especially about what happens when a result doesn't fit.

You met the integer types briefly in Data Types. This lesson goes deeper: the real ranges of each type, the fixed-width types from <stdint.h>, how to print them portably, how to write integer literals (including C23's digit separators and binary literals), and the crucial difference between unsigned wraparound and signed overflow.

The standard integer types and their ranges

C has five standard signed integer types, each with an unsigned partner:

Type Minimum width On our server Range on our server
signed char 8 bits 8 โˆ’128 โ€ฆ 127
short 16 bits 16 โˆ’32,768 โ€ฆ 32,767
int 16 bits 32 โˆ’2,147,483,648 โ€ฆ 2,147,483,647
long 32 bits 64 about ยฑ9.2 ร— 10ยนโธ
long long 64 bits 64 about ยฑ9.2 ร— 10ยนโธ

For an unsigned type with n bits the range is 0 โ€ฆ 2โฟ โˆ’ 1, so unsigned int goes up to 4,294,967,295. Signed types use two's complement (required since C23, and used by every real machine long before that), which gives a range of โˆ’2โฟโปยน โ€ฆ 2โฟโปยน โˆ’ 1. Notice the asymmetry: there's one more negative value than positive.

You don't have to memorize these numbers. <limits.h> defines them as macros, which Type Limits covers in full:

C Integer ranges from limits.h

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Each specifier matches its type: %d/%u for int/unsigned, %ld for long, %lld/%llu for long long. (short and char values are promoted to int when passed to printf, so %d and %u work for them.)

Fixed-width types: stdint.h

Because int and long vary between platforms, C99 added <stdint.h> with types whose names state their size:

Type Meaning
int8_t, int16_t, int32_t, int64_t Exactly 8/16/32/64 bits, signed
uint8_t, uint16_t, uint32_t, uint64_t Exactly 8/16/32/64 bits, unsigned
int_least16_t, uint_least32_t, โ€ฆ The smallest type with at least that many bits
int_fast32_t, uint_fast16_t, โ€ฆ The fastest type with at least that many bits
intmax_t, uintmax_t The widest integer type available
intptr_t, uintptr_t Big enough to hold a pointer converted to an integer (optional)

The exact-width types are technically optional (a machine without 8-bit bytes can't provide int8_t), but every mainstream platform has them. Use them for binary file formats, network protocols, hardware registers and hashing, anywhere the exact bit count matters. For ordinary counters, plain int and size_t are still the idiomatic choice.

Printing them portably: inttypes.h

What specifier prints an int64_t? On Linux it's a long (%ld); on Windows it's a long long (%lld). To avoid guessing, <inttypes.h> provides format macros that expand to the right letters. They're string literals, so you paste them into the format string by placing them next to other strings:

C Fixed-width types and their format macros

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

"%" PRId64 "\n" looks odd, but C joins adjacent string literals at compile time, so on our server it becomes "%ld\n". The naming pattern is PRI + the specifier letter (d, i, u, x, X, o) + the size. For reading with scanf there's a matching SCN family (SCNd64, โ€ฆ).

Writing integer literals

An integer literal can be written in several bases:

Form Example Value
Decimal 255 255
Hexadecimal (0x) 0xFF 255
Octal (leading 0) 0377 255
Binary (0b, C23) 0b11111111 255

A leading zero means octal. int code = 010; stores 8, not 10. That catches people who pad numbers like 007 or 0640 for alignment. (C2y, the next standard, adds the clearer 0o prefix, but don't rely on it yet.)

Suffixes choose the literal's type: u or U for unsigned, l/L for long, ll/LL for long long, combinable as ULL. Without a suffix, a decimal literal gets the first of int, long, long long that can hold it. C23 also adds wb/uwb for the new _BitInt(N) types, which you'll rarely need.

Digit separators (C23) make long numbers readable: a single quote between digits is ignored by the compiler. 1'000'000 is one million, and 0b1010'0101 groups bits into nibbles.

C Literals in every base

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Binary literals and digit separators need C23 (GCC 11+ / Clang 13+ with -std=c23, or -std=c2x on older versions). GCC and Clang have accepted 0b as an extension for years. For portable C17 code, stick to hex for bit patterns.

Unsigned arithmetic wraps around

Unsigned integers follow modular arithmetic: every result is reduced modulo 2โฟ. Adding 1 to the maximum gives 0, and subtracting 1 from 0 gives the maximum, like an odometer rolling over. This is fully defined by the standard and identical on every platform, which is why hash functions, checksums and random number generators use unsigned types.

C Unsigned wraparound is defined

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The counter prints 250 251 252 253 254 255 0 1, and 0u - 1 prints 4294967295. The hash at the end (FNV-1a) multiplies by a large number again and again; the high bits that don't fit simply vanish, exactly as intended.

The flip side: wraparound turns bugs into silent nonsense. A loop like for (unsigned i = n - 1; i >= 0; i--) never ends, because an unsigned i is always >= 0; after 0 it wraps to 4294967295. GCC warns about that comparison with -Wextra.

Signed overflow is undefined behavior

For signed types the rule is completely different. If a signed calculation produces a value outside the type's range, the behavior is undefined. The standard makes no promise at all: the program may wrap around, crash, or behave in ways that seem impossible, because the optimizer is allowed to assume overflow never happens.

That assumption is real. Given x + 1 > x with an int x, GCC simply replaces the expression with 1 (true), because for every value where it's defined, it is true. A check like if (a + b < a) written to "detect" overflow may be deleted for the same reason.

Check before you calculate, never after. Once the overflowing operation has run, it's too late: the program is already undefined. Compare against the limits first, or use C23's ckd_add, ckd_sub and ckd_mul. Both techniques are shown in Type Limits.

Here's the safe way to handle a value that may get too big, without ever overflowing:

C Avoiding signed overflow

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Look closely at the last calculation. It's correct only because the LL is on the last factor and the earlier products still fit in an int (30 ร— 86,400 = 2,592,000). Multiplication runs left to right, so with days * 24 * 60 * 60 * 1000 and no suffix at all, the final multiply happens in int and overflows, even though the result is stored in a long long. The safe habit is to widen the first operand: (long long)days * 24 * 60 * 60 * 1000.

Integer division and remainders

Dividing two integers produces an integer: the fraction is thrown away, rounding toward zero. So 7 / 2 is 3 and -7 / 2 is โˆ’3. The % operator gives the matching remainder, which takes the sign of the left operand: -7 % 2 is โˆ’1. Dividing by zero, or computing INT_MIN / -1, is undefined behavior. The Arithmetic Operators lesson covers division in depth.

Choosing an integer type

  • int for ordinary values that comfortably fit in ยฑ2 billion.
  • size_t for sizes, counts of elements and array indexes.
  • long long or int64_t for values that can exceed about 2 billion.
  • uint8_t/uint32_t/โ€ฆ for bytes, bit masks, hashes and binary formats.
  • Unsigned only when you want modular arithmetic or bit manipulation, not merely because a value "shouldn't be negative". Mixing signed and unsigned values causes surprises, as Type Conversions shows.
  • short and signed char mainly to save memory in large arrays; arithmetic on them happens in int anyway.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does printf("%d\n", 017); print?

    A leading zero makes the literal octal. Octal 17 is 1 ร— 8 + 7 = 15.
  2. 2. uint8_t c = 255; c++; What is c now?

    Unsigned arithmetic wraps around modulo 2 to the power of n. For 8 bits that's modulo 256, so 255 + 1 becomes 0. This is well-defined.
  3. 3. int x = INT_MAX; x = x + 1; What does the C standard say happens?

    Signed overflow is undefined behavior. Many machines would wrap to INT_MIN, but the compiler may assume it never happens and optimize accordingly, so you can't rely on any result.
  4. 4. Why use PRId64 from <inttypes.h> to print an int64_t?

    The underlying type of int64_t differs between platforms, so the right specifier (%ld or %lld) differs too. The macro expands to whichever is correct.
  5. 5. With a 32-bit int, which expression avoids overflow when n is 100000?

    Converting the first operand makes the multiplication itself happen in long long. In the other options, n * n is computed in int first and overflows before any conversion.

Summary

  • C has signed char, short, int, long and long long, each with an unsigned version; <limits.h> gives their exact ranges.
  • <stdint.h> provides exact-width types (int32_t, uint8_t, โ€ฆ); print them with <inttypes.h> macros like PRId64.
  • Literals can be decimal, hex (0x), octal (leading 0) or binary (0b, C23); suffixes u, L, LL set the type; C23 allows digit separators like 1'000'000.
  • Unsigned arithmetic wraps around and is well-defined; signed overflow is undefined behavior, so check limits before calculating or use a wider type.
  • Widen the first operand of a long calculation so every step happens in the wide type.

Next: Floating-Point Numbers, where you'll learn how float and double store fractions and why 0.1 + 0.2 isn't exactly 0.3.