Platform dependency of sizes
What you'll learn: Why the amount of memory an integer occupies can change depending on the computer or operating system you are using.
The flexible ruler
In many programming languages, an integer is always exactly the same size. In C, things are a bit more "fluid." You can think of the C standard as a set of blueprints that says a door must be at least six feet tall, but the builder is free to make it seven or eight feet tall if they have the extra space.
In C, the language defines minimum ranges for types like int or long, but it doesn't strictly dictate their exact size in bytes. Instead, the size is often determined by your platform—the combination of your computer’s hardware (CPU) and your operating system.
Why does this happen?
C was designed to be incredibly fast and efficient. To achieve this, it lets the compiler choose a size for an int that fits most naturally with the computer's architecture.
On an old 16-bit computer from the 1980s, an int might only be 2 bytes. On a modern 64-bit desktop, an int is typically 4 bytes. If you move your code to a tiny microchip inside a microwave, the size might shrink again to save memory. This flexibility allows C to run on everything from a supercomputer to a digital watch, but it means you can never assume an int is the same size everywhere.
Measuring your platform
Because you can't guess the size, C provides a special tool called sizeof(). This tells you exactly how many bytes a type occupies on your specific machine right now.
#include <stdio.h>
int main() {
// We use %zu to print sizes in C
printf("On this machine:\n");
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of long: %zu bytes\n", sizeof(long));
return 0;
}
If you run this code on a Windows PC, you might see that a long is 4 bytes. Run the exact same code on a Linux Mac, and a long might be 8 bytes! This is why platform dependency matters: if you write a program that expects a long to always be 4 bytes, your program might behave strangely or crash when moved to a different system.
Key takeaway
In C, the size of integer types is not fixed; it adapts to your specific hardware and operating system to ensure maximum performance.