- What
sizeofis — a compile-time operator that returns the size in bytes of a type or expression - The correct format specifier —
%zu— for printingsizeofresults - The typical sizes of
char,int,float,double, and their variants on modern systems - Which sizes are guaranteed by the C standard and which are platform-dependent
- How to use the fixed-width types from
<stdint.h>for portable code - Why
sizeof(array)differs fromsizeof(pointer)
The sizeof operator, and why sizes vary
The C standard does not fix the size of most types — it only guarantees minimums and relative orderings. char is exactly 1 byte (by definition), short is at least 2 bytes and at least as large as char, int is at least 2 bytes and at least as large as short, and so on. On a given system, the exact sizes are determined by the platform's "data model" — a convention that the compiler and operating system agree on.
The most common data models today are LP64 (used on 64-bit Linux and macOS — long and pointer are 64 bits) and LLP64 (used on 64-bit Windows — long and int are 32 bits, only long long and pointer are 64 bits). This is why sizeof(long) is 8 on Linux but 4 on Windows, even though both are 64-bit systems.
The sizeof operator tells you exactly how many bytes a type occupies on the current platform. It's a compile-time operator (except in C99 when applied to a variable-length array) — the compiler determines the result during compilation, not at runtime. This means sizeof costs nothing at runtime; it's effectively a compile-time constant.
The result of sizeof has type size_t, an unsigned integer type defined in <stddef.h>. To print it, use %zu — the z is a length modifier meaning "size_t-sized," and u means unsigned. Using %d would be a type mismatch (undefined behavior, usually with a warning from the compiler).
💡 In one line: sizeof(type) returns the byte size of a type on the current platform, as a size_t. Print it with %zu. The size of int, long, and pointers varies by platform; char is always 1 byte, and double is almost always 8.
Reference table of typical sizes
Here's what you can expect on the most common platforms (64-bit Linux, macOS, Windows):
| Type | 64-bit Linux/macOS | 64-bit Windows | 32-bit (any) |
|---|---|---|---|
char | 1 byte | 1 byte | 1 byte |
short | 2 bytes | 2 bytes | 2 bytes |
int | 4 bytes | 4 bytes | 4 bytes |
long | 8 bytes | 4 bytes | 4 bytes |
long long | 8 bytes | 8 bytes | 8 bytes |
float | 4 bytes | 4 bytes | 4 bytes |
double | 8 bytes | 8 bytes | 8 bytes |
pointer | 8 bytes | 8 bytes | 4 bytes |
Some observations:
charis always 1 byte. The C standard defines a byte as "the size of achar," so this is true by definition.shortis 2 bytes on virtually every modern system, though the standard only requires 2. Some embedded systems use larger.intis 4 bytes on virtually every modern system. Embedded systems sometimes use 2 bytes; mainframes may use larger.longvaries — 8 on 64-bit Linux/macOS, 4 on Windows (even on 64-bit Windows) and 32-bit Linux. This is the biggest portability gotcha.long longis always at least 8 bytes and is 8 on every modern platform.floatis 4 bytes,doubleis 8 bytes on virtually every modern system.- Pointers match the platform's address width — 8 bytes on 64-bit systems, 4 on 32-bit.
🎯 Key idea: the C standard doesn't fix the size of int or long — it only requires minimums. For portable code that needs an exact size, use int8_t, int16_t, int32_t, int64_t from <stdint.h>. For determining sizes at compile time, use sizeof. Never hardcode sizes like "4" or "8."
Complete C Program
Program: print sizeof for each fundamental type
#include <stdio.h>
int main(void) {
printf("--- Fundamental data type sizes on this platform ---\n");
printf("sizeof(char) = %zu byte(s)\n", sizeof(char));
printf("sizeof(short) = %zu bytes\n", sizeof(short));
printf("sizeof(int) = %zu bytes\n", sizeof(int));
printf("sizeof(long) = %zu bytes\n", sizeof(long));
printf("sizeof(long long) = %zu bytes\n", sizeof(long long));
printf("sizeof(float) = %zu bytes\n", sizeof(float));
printf("sizeof(double) = %zu bytes\n", sizeof(double));
printf("sizeof(long double) = %zu bytes\n", sizeof(long double));
printf("\n--- Variants ---\n");
printf("sizeof(unsigned int) = %zu bytes\n", sizeof(unsigned int));
printf("sizeof(unsigned long) = %zu bytes\n", sizeof(unsigned long));
printf("sizeof(unsigned long long) = %zu bytes\n", sizeof(unsigned long long));
printf("\n--- sizeof on variables ---\n");
int i = 42;
double d = 3.14;
char c = 'X';
printf("sizeof(i) = %zu bytes\n", sizeof(i));
printf("sizeof(d) = %zu bytes\n", sizeof(d));
printf("sizeof(c) = %zu byte(s)\n", sizeof(c));
return 0;
}
The program uses sizeof on each type name and on each variable. The results should be identical for a variable and its type — sizeof(i) equals sizeof(int), because i is an int. The sizeof operator doesn't evaluate its operand in the case of variable-length arrays, but for fixed-size types it's a compile-time constant.
And here's a more advanced version that also checks the fixed-width types and demonstrates the difference between sizeof(array) and sizeof(pointer):
Variant: fixed-width types and array vs. pointer
#include <stdio.h>
#include <stdint.h>
int main(void) {
printf("--- Fixed-width types (from <stdint.h>) ---\n");
printf("sizeof(int8_t) = %zu byte(s)\n", sizeof(int8_t));
printf("sizeof(int16_t) = %zu bytes\n", sizeof(int16_t));
printf("sizeof(int32_t) = %zu bytes\n", sizeof(int32_t));
printf("sizeof(int64_t) = %zu bytes\n", sizeof(int64_t));
printf("\n--- Array vs. pointer ---\n");
int arr[10];
int *ptr = arr;
printf("sizeof(arr) = %zu bytes (= 10 * sizeof(int))\n", sizeof(arr));
printf("sizeof(ptr) = %zu bytes (just the pointer size)\n", sizeof(ptr));
return 0;
}
The sizeof(arr) gives the total bytes of the array (10 × 4 = 40 on typical systems), while sizeof(ptr) gives just the size of the pointer (8 on 64-bit). This is a famous subtlety — arrays decay to pointers when passed to functions, so sizeof inside a function that takes an array parameter gives the pointer size, not the array size. It's why you must pass the array length separately.
Program Output
Here is typical output from the first program on a 64-bit Linux system:
--- Fundamental data type sizes on this platform ---
sizeof(char) = 1 byte(s)
sizeof(short) = 2 bytes
sizeof(int) = 4 bytes
sizeof(long) = 8 bytes
sizeof(long long) = 8 bytes
sizeof(float) = 4 bytes
sizeof(double) = 8 bytes
sizeof(long double) = 16 bytes
--- Variants ---
sizeof(unsigned int) = 4 bytes
sizeof(unsigned long) = 8 bytes
sizeof(unsigned long long) = 8 bytes
--- sizeof on variables ---
sizeof(i) = 4 bytes
sizeof(d) = 8 bytes
sizeof(c) = 1 byte(s)
All sizes are as expected. The long is 8 bytes (LP64 model), long double is 16 bytes (extended precision — 80-bit extended stored in 16 bytes with padding on x86-64). The variables match their types exactly.
On 64-bit Windows with MSVC (LLP64), you'd see:
sizeof(char) = 1 byte(s)
sizeof(short) = 2 bytes
sizeof(int) = 4 bytes
sizeof(long) = 4 bytes ← note: 4, not 8!
sizeof(long long) = 8 bytes
sizeof(float) = 4 bytes
sizeof(double) = 8 bytes
sizeof(long double) = 8 bytes ← note: 8, not 16!
Two differences: long is 4 bytes (not 8), and long double is 8 bytes (not 16). This is the LLP64 model — a deliberate choice by Microsoft to preserve compatibility with 32-bit Windows. On 32-bit systems (any OS), long is 4 bytes and pointer is 4 bytes.
And the variant's output:
--- Fixed-width types (from) --- sizeof(int8_t) = 1 byte(s) sizeof(int16_t) = 2 bytes sizeof(int32_t) = 4 bytes sizeof(int64_t) = 8 bytes --- Array vs. pointer --- sizeof(arr) = 40 bytes (= 10 * sizeof(int)) sizeof(ptr) = 8 bytes (just the pointer size)
The fixed-width types give the same sizes you'd expect — that's their whole point. The array-vs-pointer difference shows that sizeof operates on the actual storage, not on what the name "looks like" — arr is 40 bytes (10 elements × 4 bytes), while ptr is 8 bytes (just the pointer).
🔍 Try this: run the program on different platforms (Linux, macOS, Windows), or compile with -m32 on Linux to see the 32-bit sizes. On 32-bit, long is 4 bytes and pointers are 4 bytes. Then think about portability — a program that assumes long is 8 bytes on Linux and moves to Windows will silently break. Use int64_t instead.
Step-by-Step Explanation
Let's walk through the program and the underlying concepts.
1. The sizeof operator
sizeof(type) returns the number of bytes that a value of type occupies. It's an operator (like + or *), not a function. The parentheses are usually required (for the type-name form), but for a variable you can write sizeof x without parentheses. Both forms are equivalent for the common cases.
The result type is size_t, which is defined in <stddef.h> (and pulled in indirectly by <stdio.h>). size_t is typically an unsigned 64-bit integer on 64-bit systems, or 32-bit on 32-bit systems. Use %zu to print it — anything else is technically undefined behavior (though the compiler usually warns).
2. The char is always 1 byte
The C standard defines sizeof(char) as 1 — that's the definition of a byte. On every platform, char is exactly 1 byte. What varies is the number of bits per byte: the standard requires at least 8, and virtually every modern system uses exactly 8. On some old or specialized hardware, a byte could be 9 or 16 bits, but you'll never see that in practice.
The related type char is signed on most systems, but it's implementation-defined — some compilers treat char as unsigned by default. If you need explicit signedness, use signed char or unsigned char. This is a subtlety that rarely matters in practice but is worth knowing.
3. Why long varies
The size of long is the biggest portability gotcha in C. On Unix-like systems (Linux, macOS), the LP64 model is used: long and pointers are 64 bits. On Windows, the LLP64 model is used: long is 32 bits (same as int), but pointers are 64 bits. The Windows choice was made for backward compatibility — switching long to 64 bits would have broken many existing Windows programs that assumed 32-bit long.
This is why portable code uses int64_t or long long instead of long when a 64-bit integer is needed. long long is 64 bits on every modern platform; int64_t is guaranteed to be exactly 64 bits by <stdint.h>.
4. Why double is almost always 8
The size of double is almost always 8 bytes because the IEEE 754 floating-point standard (adopted by virtually every modern platform) specifies 64-bit doubles. float is 32 bits, long double is 80 bits (x86 extended) or 128 bits (IEEE quad precision) — but the actual size of long double varies: 16 bytes on x86-64 Linux, 8 bytes on Windows MSVC (where long double is identical to double), 16 bytes on ARM64.
For maximum portability of floating-point code, use double and avoid long double. If you need exact precision guarantees, use the types in <float.h> or the decimal types in C23 (if available). For most numerical work, double is the standard choice.
5. The %zu format specifier
%zu is the portable way to print a size_t. The z is a length modifier meaning "the corresponding unsigned integer type is size_t," and u means "print as unsigned decimal." So %zu prints a size_t in decimal. Using %d instead would truncate the value on some systems (if size_t is wider than int), and using %lu would work on most systems but isn't portable (because size_t isn't necessarily unsigned long).
The %zu specifier was added in C99. Before that, the portable way was to cast to unsigned long and use %lu: printf("%lu", (unsigned long)sizeof(int));. That's still valid for C89 code, but %zu is cleaner and correct for C99+.
6. sizeof on a variable vs. on a type
sizeof(i) and sizeof(int) give the same result when i is an int — because the compiler knows i's type and just looks up its size. The operator doesn't read i's value; it only uses its type. This is important: sizeof(i++) does not increment i, because sizeof doesn't evaluate its operand. (Well, in C99 with variable-length arrays, sizeof might evaluate the array's size expression, but not the elements.)
7. The array-vs-pointer subtlety
sizeof(arr) gives the total bytes of the array (40 for a 10-element int array on a 32-bit int platform). sizeof(ptr) gives the size of the pointer (8 bytes on 64-bit). They're very different, because arrays and pointers are very different things — but in most contexts (like passing to a function), an array decays to a pointer. So inside a function that received int arr[] as a parameter, sizeof(arr) gives the pointer size (8), not the array size (40). This is a source of subtle bugs, especially in generic functions.
The fix: pass the array length as a separate parameter, or use a struct that contains a fixed-size array (where sizeof gives the correct size). Modern C code often does the latter — wrapping arrays in structs to preserve size information.
8. Why sizeof matters
sizeof is essential in several places:
- Allocating memory:
malloc(sizeof(int) * n)allocates space fornintegers. Using a hardcoded size likemalloc(4 * n)breaks on platforms whereintisn't 4 bytes. - Serializing data: to write a struct to a file in binary, you use
fwrite(&struct_var, sizeof(struct_var), 1, file). - Array lengths:
sizeof(arr) / sizeof(arr[0])gives the number of elements — but only for arrays in scope, not for pointers. - Type-agnostic code: macros like
#define NELEMS(a) (sizeof(a) / sizeof((a)[0]))work for any array type. - Determining struct layout: padding and alignment can make
sizeof(struct)larger than the sum of its fields. Understanding this requiressizeof.
9. Fixed-width types for portability
The types in <stdint.h> guarantee exact widths: int8_t, int16_t, int32_t, int64_t for signed, and uint8_t, etc. for unsigned. They are optional — a platform might not have int64_t if it can't support 64-bit integers — but any modern platform provides them. Use them whenever you need a specific number of bits, especially for data serialization, protocol implementations, and cross-platform binary formats.
For maximum portability, use these types instead of short, int, long. The only exception is when interacting with system APIs that expect specific types (e.g., printf returns int, not int32_t). In those cases, follow the API.
10. A caveat: sizeof and padding
For structs and unions, sizeof includes padding that the compiler inserts for alignment. For example, a struct with a char followed by an int might be 8 bytes, not 5 — because the compiler inserts 3 bytes of padding between the char and the int so the int is aligned to a 4-byte boundary. This is a subtlety you'll encounter when you learn about structs, and sizeof is how you inspect the actual size. sizeof returns the padded size, which is what matters for memory allocation.
🎯 Key idea: sizeof is a compile-time operator that gives you the exact byte size of any type or expression, on the current platform. Use it to write portable code — never hardcode sizes. Use %zu to print the result. Use <stdint.h> types when you need exact widths across platforms.
Frequently Asked Questions
1. Why does sizeof(long) differ between Linux and Windows?
Because the two platforms use different data models. Linux and macOS use the "LP64" model, where long and pointers are 64 bits. Windows uses "LLP64," where long is 32 bits (same as int), but long long and pointers are 64 bits. Microsoft chose LLP64 for backward compatibility — switching long to 64 bits would break existing 32-bit Windows programs. The upshot: if you need a 64-bit integer, use long long or int64_t, not long.
2. Why is sizeof(char) always 1?
Because the C standard defines a byte as the size of a char. So by definition, sizeof(char) = 1 byte. What varies is how many bits are in a byte: the standard requires at least 8 bits, and virtually every modern platform uses exactly 8. On some exotic hardware, a byte could be 9 or 16 bits, but that's rare.
3. What's the difference between sizeof(arr) and sizeof(ptr) when both refer to the same array?
sizeof(arr) gives the total size of the array — 40 bytes for a 10-element int array on typical systems. sizeof(ptr) gives the size of the pointer variable — 8 bytes on 64-bit. The array and the pointer have different types (int[10] vs int*) and different sizes. This matters when you pass an array to a function — it decays to a pointer, so sizeof(arr) inside the function gives the pointer size (8), not the array size (40). You must pass the length separately.
4. Why use %zu instead of %d or %lu for sizeof?
Because sizeof returns size_t, which is not necessarily int or unsigned long. %zu is the C99 standard format specifier for size_t. Using %d would be undefined behavior (the type is wrong), and using %lu would work on most systems but isn't portable (because size_t might not be unsigned long on every platform). %zu is correct everywhere.
5. Why is sizeof(int) sometimes 2 and sometimes 4?
Because the C standard doesn't fix the size of int — it only requires at least 16 bits (2 bytes). On virtually all modern systems, int is 32 bits (4 bytes). On some embedded systems (e.g., old 8-bit microcontrollers), it might be 16 bits. On some mainframes, it could be 64 bits. The result is always ≥ 2 bytes, but the exact value depends on the platform. If you need an exact size, use int16_t or int32_t.
6. Can I use sizeof with an expression, not just a type?
Yes. sizeof x works if x is a variable — the result is the size of the variable's type. sizeof x + 1 is ambiguous (does the + 1 apply to the expression before sizeof, or is it part of the operand?). For clarity, always use parentheses: sizeof(x) + 1. Also, sizeof doesn't evaluate its operand if the operand has no side effects — sizeof(x++) does not increment x. This is true for fixed-size types; for variable-length arrays (C99), sizeof may evaluate the array's size expression.
💡 Tip: once this works, try printing the maximum and minimum values of each type. Include <limits.h> and print INT_MAX, INT_MIN, CHAR_MAX, SHRT_MIN, etc. For floats, include <float.h> and print FLT_MAX, DBL_MIN, etc. This gives you a concrete sense of what range each type can hold — essential for choosing the right type in real code.