Skip to main content
Be Bitwise
CC Basics & Memory

Variables & Types

Every piece of data in a C program wears a type. The type answers two questions for the compiler: how many bytes to set aside, and how to read the bits once they're there.

Fundamental Types

TypeSize (typical)Range
char1 byte-128 to 127 (or 0 to 255)
short2 bytes-32,768 to 32,767
int4 bytesabout -2 billion to 2 billion
long4 or 8 bytesat least -2 billion to 2 billion
float4 bytes~7 decimal digits of precision
double8 bytes~15 decimal digits of precision

Declaring Variables

In C, you must declare a variable before using it. The declaration tells the compiler the type and name:

c
int age = 25;
float temperature = 36.6f;
char grade = 'A';

Memory Implications

When you write int x = 42;, the compiler:

  1. Carves 4 bytes off the stack for x (at least without optimisation; an optimising compiler will often keep x in a register instead).
  2. Drops the binary form of 42 into those bytes.
  3. Notes the address where x now lives.

Python and JavaScript work differently. There, a name is a tag pointing at some object floating around on the heap. In C, x is the memory. No indirection, no detour.

Key Concept

A C variable is a named patch of memory with a type stamped on it. The type fixes the size and tells the compiler how to read the bytes. That direct mapping between name and address is exactly where C's power, and most of its sharp edges, come from.

Type Sizes Are Not Guaranteed

Those sizes above? Common, but the C standard makes no promises beyond minimum widths and relative ordering. When you need a precise width, reach for <stdint.h>:

c
#include <stdint.h>
int8_t small = 42; // exactly 8 bits
int32_t medium = 42; // exactly 32 bits
int64_t large = 42; // exactly 64 bits

The bits don't care what you meant, either. Before you run this, predict each line. Two of them surprise almost everyone.

Try itPredict every line first, then run it. Which results would change on a machine where char is unsigned?
C (GCC 9.2.0)

-1 < 1u prints 0 because the comparison mixes signed and unsigned, so C converts -1 to unsigned int and it becomes 4,294,967,295. That's the root of a whole family of length-check bugs you'll meet in the security modules.

Signed types store negative numbers in two's complement, where the top bit counts as -128 in an 8-bit value rather than +128. The bits below start as int8_t x = 127. Add one to it by hand: click the rightmost 1 and work the carry left (every 1 becomes 0, then the top bit becomes 1).

27
26
25
24
23
22
21
20
Decimal127
Hex0x7F
Binary01111111

The pattern 10000000 reads as -128. That's what x++ leaves in an int8_t on GCC and Clang. Strictly, the arithmetic happens in int and squeezing 128 back into 8 bits is implementation-defined, unlike INT_MAX + 1, which is undefined.

The sizeof Operator

You can check the size of any type or variable at compile time:

c
printf("int is %zu bytes\n", sizeof(int));
printf("x is %zu bytes\n", sizeof(x));

Guess the numbers for a 64-bit Linux machine before you run this. Then compare with the table: long is 8 bytes here because Linux uses the LP64 model, while 64-bit Windows keeps long at 4 (LLP64).

Try itPredict each size, run it, then add a line for a type of your own choosing.
C (GCC 9.2.0)

You'll lean on sizeof constantly once memory allocation enters the picture, which is the next stop.