Variables & Data Types
The Strict Librarian
Imagine a librarian who runs the tightest ship in town. Before you can put anything on a shelf, you must tell her exactly what kind of item it is and how much space it needs. A paperback? Shelf A. An encyclopedia? Shelf B. No guessing, no surprises.
That's C. Unlike Python or JavaScript where you just toss values around and the language figures it out, C demands you declare the type up front. This strictness is what makes C fast β the compiler knows exactly how many bytes to reserve for each variable.
The Core Data Types
C gives you a handful of fundamental types, each with a specific size in memory:
charβ 1 byte. Holds a single character like'A'or a small integer (-128 to 127).intβ Typically 4 bytes. Your go-to for whole numbers.floatβ 4 bytes. Decimal numbers with ~6-7 digits of precision.doubleβ 8 bytes. Decimal numbers with ~15-16 digits of precision. The default for floating-point math.shortβ Typically 2 bytes. A smaller integer when memory is tight.longβ Typically 8 bytes (on 64-bit systems). For whenintisn't big enough.
You can also add unsigned in front of integer types to say "no negative numbers allowed" β which doubles the positive range.
Declaring Variables
sizeof β Measuring the Shelves
Ever wonder exactly how many bytes each type takes? The sizeof operator tells you. This is important because sizes can vary between systems β an int might be 2 bytes on an old embedded device or 4 bytes on your laptop.
Using sizeof()
Type Limits & Overflow
Every type has a range. An int can hold roughly -2.1 billion to +2.1 billion. What happens if you go past the limit? The value wraps around β like an odometer rolling past 999,999 back to 000,000. This is called overflow, and it's a silent bug that C won't warn you about.
Type Limits and Overflow
bool type (before C99). You can #include <stdbool.h> to get true and false, but under the hood they're just 1 and 0. There's also no string type β strings in C are arrays of char ending with a null character '\0'. This is a major difference from most modern languages!Constants
Sometimes you want a variable that never changes β like the speed of light or the value of pi. Use const to make a variable read-only, or #define for a preprocessor constant.