Lesson 32 +10 XP

Integer Numbers and Modifiers

Integer Numbers and Modifiers

Integers are the whole numbers. C gives you several flavors via modifiers.

int

The plain int is signed and usually 4 bytes (about -2.1 billion to +2.1 billion):

int score = 1000000;

unsigned

unsigned gives up negatives to get a bigger positive maximum:

unsigned int big = 4000000000U;

short and long

  • short - smaller, often 2 bytes.
  • long - larger, often 8 bytes on 64-bit machines.
  • long long - even bigger.
short int small = 100;
long int huge = 1000000000;
long long int enormous = 9000000000000000000LL;

signed vs unsigned

  • signed can hold negatives and positives (default for int).
  • unsigned only holds 0 and positives.

The %d family of specifiers

  • %d / %i - int
  • %u - unsigned int
  • %ld - long
  • %lld - long long
  • %hd - short

TL;DR

  • int is the common whole-number type (usually 4 bytes).
  • unsigned swaps negatives for a bigger positive range.
  • short, long, long long resize the integer.
  • Specifiers: %d, %u, %ld, %lld.