C Basics · beginner · ~8 min
Declare, assign, and reuse variables of basic types.
A variable is a named region of memory with a fixed type. You declare it once (int counter;), assign values into it (counter = 0;), and read those values back (printf("%d\n", counter);). Variables let you stage intermediate results, count, and refer to data by a meaningful name instead of by its memory address.
Variables are how programs maintain state. Without them, every computation would have to be one giant expression. With them, you can break a problem into named steps — and once a value has a name, you can reason about it, print it, change it, and explain it to your future self.
Declaration reserves space: int x; says 'give me 4 bytes of memory for an int and call it x'. Initialisation sets a starting value: int x = 5;. Assignment changes the value later: x = 10;. Scope controls where the name is visible — variables declared inside { } only exist until the closing brace. Storage class controls lifetime — static int x; keeps its value across function calls; plain int x; inside a function does not.
type name; — declaration. type name = expr; — declaration + initialisation. name = expr; — reassignment. Multiple in one line: int a = 1, b = 2;. Constants use const: const double PI = 3.14159; — the compiler will refuse later assignments. Use _Bool from <stdbool.h> (or just int 0/1) for true/false.
A variable is a named storage location with a fixed type. Declare it with type name; or type name = value;. Names are case-sensitive, must start with a letter or underscore, and can't be a reserved word.
C is statically typed: the compiler enforces the type at every assignment and operation. Once declared int, a variable is an int forever in that scope.
int count = 0;
char letter = 'A';
double rate = 0.05;
count = count + 1; // 1
If a variable seems to have a wrong value, print it just before and after every assignment. Use gdb and print counter to inspect it without modifying the program. Watch out for shadowing — declaring another variable with the same name in an inner block hides the outer one.
Uninitialised stack variables hold garbage — whatever bytes were last on that part of the stack. Always assign a value before you read. int x; followed by printf("%d\n", x); is technically undefined behaviour in C — it might print 0, it might print 0x7FFE4A2A, and the optimizer is allowed to assume it can never happen.
Every program everywhere uses variables. The cleverness is in naming: good names (tcp_segment_count, bytes_remaining) make code self-documenting; bad names (x, temp, flag) make you reread the surrounding 20 lines every time.
int, assign three different values to it, and print after each. 2. Try reading a variable you never initialised — note the value, then add -Wall to the compile command and read the warning. 3. Use const int LIMIT = 10; and try to assign to it; observe the compile error.Variables are named memory cells. Declare with a type, assign before you read, and prefer descriptive names — your future self will thank you when you read your own code months later.