The sizeof operator in C returns the size in bytes of a type or expression. It is evaluated at compile time and is the standard, portable way to determine how much memory any type occupies on the current platform. Because type sizes vary between 32-bit and 64-bit systems and between compilers, you should always use sizeof rather than hardcoding sizes.
C Program to Find Size of Data Types
/* sizeof() for all C data types
* Compile: gcc -ansi -Wall -Wextra sizeof_types.c -o sizeof_types */
#include <stdio.h>
int main(void)
{
printf("Integer types:\n");
printf(" char : %d byte(s)\n", (int)sizeof(char));
printf(" signed char : %d byte(s)\n", (int)sizeof(signed char));
printf(" unsigned char : %d byte(s)\n", (int)sizeof(unsigned char));
printf(" short : %d byte(s)\n", (int)sizeof(short));
printf(" unsigned short: %d byte(s)\n", (int)sizeof(unsigned short));
printf(" int : %d byte(s)\n", (int)sizeof(int));
printf(" unsigned int : %d byte(s)\n", (int)sizeof(unsigned int));
printf(" long : %d byte(s)\n", (int)sizeof(long));
printf(" unsigned long : %d byte(s)\n", (int)sizeof(unsigned long));
printf("\nFloating-point types:\n");
printf(" float : %d byte(s)\n", (int)sizeof(float));
printf(" double : %d byte(s)\n", (int)sizeof(double));
printf(" long double : %d byte(s)\n", (int)sizeof(long double));
printf("\nPointer sizes:\n");
printf(" char * : %d byte(s)\n", (int)sizeof(char *));
printf(" int * : %d byte(s)\n", (int)sizeof(int *));
printf(" long * : %d byte(s)\n", (int)sizeof(long *));
printf(" double * : %d byte(s)\n", (int)sizeof(double *));
printf("\nDerived types:\n");
printf(" int[10] array : %d byte(s)\n", (int)sizeof(int[10]));
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra sizeof_types.c -o sizeof_types
./sizeof_types
Sample Output (64-bit Linux)
Integer types: char : 1 byte(s) signed char : 1 byte(s) unsigned char : 1 byte(s) short : 2 byte(s) unsigned short: 2 byte(s) int : 4 byte(s) unsigned int : 4 byte(s) long : 8 byte(s) unsigned long : 8 byte(s) Floating-point types: float : 4 byte(s) double : 8 byte(s) long double : 16 byte(s) Pointer sizes: char * : 8 byte(s) int * : 8 byte(s) long * : 8 byte(s) double * : 8 byte(s) Derived types: int[10] array : 40 byte(s)
On a 32-bit system or Windows MSVC, long is 4 bytes and all pointers are 4 bytes. The program will show the correct values for whatever platform you run it on.
What sizeof Returns for Every Type
| Type | 32-bit | 64-bit Linux | 64-bit Windows | C standard guarantees |
|---|---|---|---|---|
char |
1 | 1 | 1 | exactly 1 (guaranteed) |
short |
2 | 2 | 2 | ≥ 2 |
int |
4 | 4 | 4 | ≥ 2, ≥ short |
long |
4 | 8 | 4 | ≥ 4, ≥ int |
float |
4 | 4 | 4 | typically IEEE 754 single |
double |
8 | 8 | 8 | typically IEEE 754 double |
| any pointer | 4 | 8 | 8 | implementation-defined |
Code Explanation
- sizeof returns size_t —
size_tis an unsigned integer type defined in<stddef.h>(included via<stdio.h>). In C89, the correct format specifier forsize_tis not standardized. The cast(int)sizeof(...)converts to a known signed type so%dworks correctly on all C89 compilers. In C99 and later you would use%zudirectly without the cast. - sizeof works on types and expressions —
sizeof(int)takes a type name in parentheses.sizeof x(without parentheses) takes an expression — the parentheses are only required for type names. - Evaluated at compile time — sizeof is not a function call. No code runs at runtime to compute it; the compiler replaces the expression with a constant. The one exception is C99 variable-length arrays (VLAs), where the size is only known at runtime.
- sizeof(char) is always 1 — this is the one size the C standard guarantees absolutely. All other sizes are expressed in multiples of
sizeof(char), so it is the “byte” unit of the language. - sizeof(int[10]) = 10 × sizeof(int) — for arrays, sizeof returns the total size of all elements. For a 10-element int array on a 64-bit machine: 10 × 4 = 40 bytes.
Practical Uses of sizeof
- Safe memory allocation:
malloc(n * sizeof(int))instead ofmalloc(n * 4)— works correctly on any platform. - Array element count:
int arr[10]; int count = sizeof(arr) / sizeof(arr[0]);— gives 10 without hardcoding the number. - Structure padding:
sizeof(struct)includes internal padding bytes added by the compiler for alignment; it is not always the sum of member sizes. - Binary I/O:
fread(&val, sizeof(val), 1, fp);reads exactly the right number of bytes regardless of platform.
What This Program Teaches
- Platform portability — type sizes are not universal. Code that hardcodes 4 for int or 8 for a pointer breaks on different platforms. sizeof makes the code self-adapting.
- size_t and format specifiers — sizeof returns
size_t, an unsigned type. Using%ddirectly is technically wrong in C89; casting tointis the portable workaround. In C99+ use%zu. - Pointers vs integers — on 64-bit platforms all pointers are 8 bytes regardless of the pointed-to type. A
char *and adouble *have the same sizeof, because both store an address. - Compile-time constants — sizeof is evaluated by the compiler, not at runtime. This makes it usable in array dimension declarations:
char buf[sizeof(int) * 8 + 1];.
Related Programs
- Arithmetic Operators in C
- Binary to Decimal in C
- GCD and LCM in C
- Sum of Digits in C
- Armstrong Number in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.