Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 135 ยท Variables and Types

Data Types

Learn C's basic data types - char, int, long, float, double and bool - how sizeof works, why sizes vary by platform, signed vs unsigned, and how to pick a type.

Every value in a C program has a type. The type tells the compiler how many bytes the value occupies, how to interpret those bytes (a whole number, a number with a fraction, a character) and which operations make sense on it. When you write int count = 0;, the word int is doing a lot of work: it reserves memory, picks the machine instructions for arithmetic, and decides which printf specifier you must use.

C's type system is small and close to the hardware. That's a strength, because you always know roughly what the machine is doing, but it also means the language leaves some details, such as the exact size of an int, to each platform. In this lesson you'll meet every basic type, measure them with sizeof, learn what is guaranteed and what isn't, and get practical rules for choosing the right type. The next lessons then zoom in on integers, floating-point numbers, characters and booleans.

The basic types at a glance

C's built-in types fall into a few families:

Family Types Holds
Character char, signed char, unsigned char One byte: a character code or a small number
Integer short, int, long, long long (each also unsigned) Whole numbers
Floating-point float, double, long double Numbers with a fractional part
Boolean bool (C23 keyword; _Bool before that) true or false
No value void Nothing: used for functions that return nothing and for generic pointers

On top of these you can build derived types: arrays, pointers, structures, unions and functions. Those get their own chapters, starting with arrays and pointers.

Here is one variable of each basic type, printed with the matching format specifier from Printing Output with printf:

C One variable of each type

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.

A few details are worth noticing:

  • The suffixes L, LL and f on the literals tell the compiler which type the number itself has. 0.75f is a float; plain 0.75 would be a double.
  • bool prints as 1 because printf has no special boolean specifier: true is just the integer 1.
  • Writing bool, true and false with no #include works because C23 made them keywords. Older compilers need #include <stdbool.h>, as the Booleans lesson explains.

Measuring types with sizeof

The sizeof operator gives the size of a type or an expression in bytes. Its result has type size_t, an unsigned integer type, and you print it with %zu:

C Sizes on our server

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.

Our server is a 64-bit Linux machine, so you'll see char 1, short 2, int 4, long 8, long long 8, float 4, double 8, long double 16, bool 1 and pointers 8.

Two forms of sizeof exist. With a type you need parentheses: sizeof(int). With an expression they're optional: sizeof scores and sizeof(scores) mean the same thing. The second half of the example uses a classic trick: dividing the size of a whole array by the size of one element gives the number of elements. It only works on a real array, not on a pointer, which the Pointers and Arrays lesson explains.

sizeof is evaluated by the compiler, not at run time (except for variable-length arrays). The expression inside isn't even executed: sizeof(x++) does not change x.

Note: A "byte" in C is the size of a char, by definition. sizeof(char) is always exactly 1. On virtually every machine you'll meet, a byte has 8 bits; the exact number is CHAR_BIT in <limits.h>, covered in Type Limits.

What the standard guarantees

C deliberately doesn't fix the size of most types. A compiler for a tiny 16-bit microcontroller can make int 16 bits, while a compiler for a desktop makes it 32 bits. What the standard does guarantee is a minimum range for each type and an ordering between them:

Type Guaranteed at least Typical today
char 8 bits 8 bits
short 16 bits 16 bits
int 16 bits 32 bits
long 32 bits 32 or 64 bits
long long 64 bits (C99) 64 bits

And sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long). For floating-point types, double has at least the precision of float, and long double at least that of double.

The size of long is the one that trips people up. 64-bit Linux and macOS use a data model called LP64 (long and pointers are 64 bits), so long is 8 bytes. 64-bit Windows uses LLP64, where long stays at 4 bytes and only long long and pointers are 8. The same program can therefore hold different ranges in a long on two machines.

Model Used by int long long long pointer
LP64 Linux, macOS, BSD (64-bit) 32 64 64 64
LLP64 Windows (64-bit) 32 32 64 64
ILP32 Older 32-bit systems 32 32 64 32

When you need an exact width, for example in a file format or a network protocol, don't guess: use the fixed-width types from <stdint.h>, such as int32_t and uint64_t. The Integer Types lesson covers them.

Don't hard-code sizes. Code like malloc(n * 4) because "an int is 4 bytes" breaks on platforms where it isn't. Write malloc(n * sizeof *p) and let the compiler fill in the real size. You'll use this idiom constantly in Dynamic Memory.

Signed and unsigned

Every integer type, including char, comes in a signed and an unsigned version. Both use the same number of bits; they just spend them differently:

  • A signed type stores negative and positive values. A 32-bit int covers about โˆ’2.1 billion to +2.1 billion.
  • An unsigned type stores only zero and positive values, but reaches twice as high: a 32-bit unsigned int goes from 0 to about 4.3 billion.

int, short, long and long long are signed unless you write unsigned. Plain char is special: whether it's signed or unsigned depends on the platform (signed on x86, unsigned on most ARM Linux systems), which the Characters lesson explores.

C The same bits, two meanings

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 bit pattern 11001000 means 200 when read as unsigned and โˆ’56 when read as signed. Likewise, 4,000,000,000 fits comfortably in an unsigned int but, forced into an int, comes out as โˆ’294967296. (Converting an out-of-range value to a signed type is implementation-defined in C; GCC simply keeps the bits, as shown.) The rules for these conversions, and the surprises they cause when you mix signed and unsigned values, are the topic of Type Conversions and Casts.

Unsigned types are the right choice for sizes, counts that can never be negative, bit masks, and raw bytes. They're a poor choice for "numbers that just happen to be positive right now", because subtracting can wrap around to a huge value instead of going negative:

C
unsigned int stock = 3;
unsigned int sold = 5;
unsigned int left = stock - sold;   // 4294967294, not -2

Type modifiers and spelling

The type keywords can be combined, and C accepts several spellings for the same type. These are all identical:

C
long long a;
long long int b;
signed long long int c;

Likewise short = short int = signed short, and unsigned on its own means unsigned int. Most code uses the shortest spelling, which is what this tutorial does.

Two more keywords are qualifiers rather than types: const (the value can't be modified, see Constants) and volatile (the value may change behind the compiler's back, used for hardware registers and signal handlers). They attach to any type: const double rate = 0.2;.

Choosing the right type

With so many options, here are practical defaults that experienced C programmers use:

C Sensible type choices

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.

  • int for everyday whole numbers: loop counters, scores, ages. It's the natural size for the machine, and arithmetic on smaller types is converted to int anyway.
  • double for anything with a fraction. Use float only when you store huge arrays of numbers (graphics, audio) and the memory matters; its 6โ€“7 significant digits run out fast.
  • size_t for sizes, lengths and array indexes. It's what sizeof, strlen and malloc use, and it's big enough for any object.
  • long long (or int64_t) when values can exceed about 2 billion: file sizes, timestamps in milliseconds, populations.
  • Fixed-width types like uint8_t and int32_t when the exact size matters: binary file formats, hardware registers, network data.
  • Never float or double for money. Binary fractions can't represent 0.10 exactly. Store whole cents in an integer, as above, and format them when printing. Floating-Point Numbers shows why.
  • bool for flags, and char for single characters and bytes of text.

Tip: Print a value with the specifier that matches its type: %d for int, %ld for long, %lld for long long, %zu for size_t, %f for double. A mismatch is undefined behavior, but our compiler's -Wall catches it with a -Wformat warning, so read your warnings.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does sizeof(char) evaluate to?

    sizeof measures in bytes, and a byte in C is defined as the size of a char. So sizeof(char) is always 1, even on rare machines where a byte has more than 8 bits.
  2. 2. On 64-bit Windows, how many bits does a long have?

    64-bit Windows uses the LLP64 model, where long stays 32 bits. 64-bit Linux and macOS use LP64, where long is 64 bits. Use long long or int64_t when you need 64 bits everywhere.
  3. 3. int a[8]; and int is 4 bytes. What does sizeof a / sizeof a[0] give?

    sizeof a is the whole array, 32 bytes; sizeof a[0] is one element, 4 bytes. The quotient is the number of elements, 8.
  4. 4. unsigned int left = 3u - 5u; What value ends up in left with a 32-bit unsigned int?

    Unsigned arithmetic wraps around modulo 2 to the power of the number of bits, so 3 - 5 becomes 4294967296 - 2. This is well-defined, and usually a bug.
  5. 5. Which type is the best fit for storing a price like $19.99 exactly?

    Binary floating-point can't represent most decimal fractions exactly, so small errors creep in. Storing whole cents in an integer type keeps money exact.

Summary

  • C's basic types are char, the integer types short/int/long/long long, the floating types float/double/long double, bool and void.
  • sizeof gives a size in bytes as a size_t (print it with %zu); sizeof(char) is always 1.
  • Most sizes are implementation-defined; the standard guarantees minimum ranges and an order. long is 64 bits on Linux/macOS but 32 bits on Windows.
  • Each integer type has signed and unsigned versions; unsigned values can't go negative and wrap around instead.
  • Defaults: int for counts, double for fractions, size_t for sizes, long long for big values, fixed-width types for exact layouts, integers for money.

Next: Integer Types, where you'll explore ranges, fixed-width types and what happens when integers overflow.