The big difference: C is statically typed. Once a variable has a type, it can never change.
π YOU KNOW (Python): Dynamic Typing
x = 42 # x is int
x = "hello" # Now x is str - perfectly fine!
x = [1, 2, 3] # Now x is list - still fine!
βοΈ IN C: Static Typing
int x = 42; // x is ALWAYS an int
x = "hello"; // β ERROR! Type mismatch
x = 100; // β OK - still an int
- Python discovers types at runtime (slow but flexible)
- C knows types at compile time (fast but rigid)
- No type checking overhead at runtime in C
C Type Sizes (Reference Table)
| Type | Bytes | Range | Use Case |
|---|---|---|---|
char
|
1 | -128 to 127 | Single character |
int
|
4 | -2Β³ΒΉ to 2Β³ΒΉ-1 | Most integers |
long
|
8 | -2βΆΒ³ to 2βΆΒ³-1 | Large integers, pointers |
float
|
4 | ~7 decimal digits | Rarely used |
double
|
8 | ~15 decimal digits | Floating point |
bool
|
1 | 0 or 1 | Boolean (needs stdbool.h) |
void*
|
8 | N/A | Generic pointer |
β οΈ Declaration vs Initialization
int x; // DECLARATION: x exists but has GARBAGE value!
printf("%d", x); // Undefined behavior (could print anything)
int x = 0; // β
ALWAYS INITIALIZE - Safe!
π¨ CRITICAL: Uninitialized variables in C contain random garbage! This causes bugs that are extremely hard to track down.
Constants
MAX_SIZE = 100
# Convention only -
# you CAN change it!
const int MAX_SIZE = 100;
MAX_SIZE = 200; // β ERROR!
#define MAX_SIZE 100 // Macro