$ overview
overview/0x01·Foundations

Variables & Types

Declaring storage, the built-in types, and how much room each one takes.

A variable in C is a named piece of memory with a fixed type. The type tells the compiler how many bytes to reserve and how to interpret the bits stored there — this is the core trade C makes: you get precise control over memory, in exchange for stating types up front.

Declaring variables

variables.c
1int age = 19;
2float price = 4.50f;
3char grade = 'A';
4double distance = 384400.0;

A declaration reserves space; an initializer (the part after =) gives it a starting value. Uninitialized local variables hold whatever garbage was already in that memory — always initialize before you read a variable.

The built-in types

  • int — a whole number, typically 4 bytes on modern systems.
  • char — a single byte, usually holding one ASCII character.
  • float — a single-precision decimal number (about 7 significant digits).
  • double — a double-precision decimal number (about 15 significant digits), the default choice for decimal math.

These come in modified forms too: short and long change the size, and unsigned removes the sign, doubling the largest representable positive value at the cost of never going negative.

modifiers.c
1short small = 12;
2long big = 3000000000L;
3unsigned int positive_only = 42u;

How much space things take

The exact size of a type isn't fixed by the language — it depends on the platform. Use sizeof to check, rather than assuming:

sizes.c
1#include <stdio.h>
2 
3int main(void) {
4 printf("int: %zu bytes\n", sizeof(int));
5 printf("double: %zu bytes\n", sizeof(double));
6 printf("char: %zu bytes\n", sizeof(char));
7 return 0;
8}
note: sizeof is not a function call
sizeof is an operator evaluated at compile time. You'll usually see it written like a function, but it works differently — it never runs your code, it just computes a size.

Naming and constants

Variable names can contain letters, digits, and underscores, but can't start with a digit. C is case-sensitive, so total and Total are different names.

Mark a variable as unchangeable with const — the compiler will reject any code that tries to modify it afterward:

program.c
1const double PI = 3.14159;
warning: Integer division truncates
7 / 2 evaluates to 3, not 3.5, because both operands are integers. Make at least one operand a float or double if you want a fractional result — chapter 0x02 covers this in detail.