The sizeof operator in C is a unary operator used to determine the memory size (in bytes) of a variable, data type, or expression.
The size varies depending on the type and can be helpful when managing memory allocation or understanding how data is stored on your system.
Syntax of sizeof
sizeof(data_type) // To get the size of a data type sizeof(variable) // To get the size of a variable sizeof(expression) // To get the size of an expression's result
The sizeof operator returns the number of bytes, typically as an unsigned int.
1. Basic Usage of sizeof with Primitive Data Types
The sizeof operator is commonly used with primitive data types like int, float, double, and char.
#include <stdio.h> int main() { printf("Size of int: %lu bytes\n", sizeof(int)); printf("Size of float: %lu bytes\n", sizeof(float)); printf("Size of double: %lu bytes\n", sizeof(double)); printf("Size of char: %lu bytes\n", sizeof(char)); return 0; }
Explanation:
- sizeof(int) returns the size of an int type.
- sizeof(float), sizeof(double), and sizeof(char) return the sizes of their respective data types.
Output (may vary based on system architecture):
Size of int: 4 bytes Size of float: 4 bytes Size of double: 8 bytes Size of char: 1 byte
2. Using sizeof with Variables
You can use sizeof to find the size of a variable, which is especially useful for derived data types like arrays or structs.
#include <stdio.h> int main() { int a = 10; float b = 20.5; char c = 'A'; printf("Size of variable a: %lu bytes\n", sizeof(a)); printf("Size of variable b: %lu bytes\n", sizeof(b)); printf("Size of variable c: %lu byte\n", sizeof(c)); return 0; }
Explanation:
- sizeof(a) returns the size of the variable a, which is an int.
- sizeof(b) and sizeof(c) return the sizes of the float and char variables b and c.
Output:
Size of variable a: 4 bytes Size of variable b: 4 bytes Size of variable c: 1 byte
3. Using sizeof with Expressions
The sizeof operator can also be used with expressions, where it evaluates the result of the expression to determine its size in bytes.
#include <stdio.h> int main() { int x = 5; double y = 3.14; printf("Size of expression (x + y): %lu bytes\n", sizeof(x + y)); // Promoted to double return 0; }
Explanation:
- sizeof(x + y) evaluates x + y, which promotes x to a double, making the result a double.
- The sizeof operator then returns the size of a double.
Output:
Size of expression (x + y): 8 bytes
4. sizeof with Arrays
The sizeof operator is often used with arrays to determine their total size and the size of each element, which can help calculate the number of elements.
#include <stdio.h> int main() { int arr[5] = {1, 2, 3, 4, 5}; printf("Size of array: %lu bytes\n", sizeof(arr)); printf("Size of each element: %lu bytes\n", sizeof(arr[0])); printf("Number of elements in array: %lu\n", sizeof(arr) / sizeof(arr[0])); return 0; }
Explanation:
- sizeof(arr) gives the total size of the array in bytes.
- sizeof(arr[0]) gives the size of each element in the array.
- Dividing the total size by the size of one element calculates the number of elements in the array.
Output:
Size of array: 20 bytes Size of each element: 4 bytes Number of elements in array: 5
5. Using sizeof with Pointers
When used with pointers, sizeof returns the size of the pointer itself (the address), not the data it points to.
#include <stdio.h> int main() { int *ptr; int arr[10]; printf("Size of int pointer: %lu bytes\n", sizeof(ptr)); printf("Size of int array pointer: %lu bytes\n", sizeof(arr)); return 0; }
Explanation:
- sizeof(ptr) returns the size of a pointer, typically 4 or 8 bytes depending on the system’s architecture.
- sizeof(arr) returns the size of the entire array when used directly on the array.
Output (may vary based on system architecture):
Size of int pointer: 8 bytes Size of int array pointer: 40 bytes
6. sizeof with Structures
The sizeof operator is useful for structures to determine the memory needed to store a structure variable. Note that the size may include padding for alignment purposes.
Example: Using sizeof with a Structure
#include <stdio.h> struct Person { char name[50]; int age; double height; }; int main() { struct Person person; printf("Size of struct Person: %lu bytes\n", sizeof(person)); printf("Size of name in struct: %lu bytes\n", sizeof(person.name)); printf("Size of age in struct: %lu bytes\n", sizeof(person.age)); printf("Size of height in struct: %lu bytes\n", sizeof(person.height)); return 0; }
Explanation:
- sizeof(person) returns the total size of the Person structure.
- sizeof(person.name), sizeof(person.age), and sizeof(person.height) return the sizes of individual members.
Output (may vary based on system and structure padding):
Size of struct Person: 64 bytes Size of name in struct: 50 bytes Size of age in struct: 4 bytes Size of height in struct: 8 bytes
7. sizeof with Dynamically Allocated Memory
When dynamically allocating memory, sizeof helps determine how much memory to allocate.
#include <stdio.h> #include <stdlib.h> int main() { int *arr; int n = 5; arr = (int *)malloc(n * sizeof(int)); if (arr == NULL) { printf("Memory allocation failed\n"); } else { printf("Memory allocated for %d integers: %lu bytes\n", n, n * sizeof(int)); } free(arr); return 0; }
Explanation:
- n * sizeof(int) calculates the total memory needed for n integers.
- malloc allocates this memory, and free releases it afterward.
Output:
Memory allocated for 5 integers: 20 bytes
8. sizeof in Macro Definitions
The sizeof operator is useful in macros to create more generic code.
#include <stdio.h> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) int main() { int arr[10]; double nums[15]; printf("Number of elements in int array: %lu\n", ARRAY_SIZE(arr)); printf("Number of elements in double array: %lu\n", ARRAY_SIZE(nums)); return 0; }
Explanation:
- ARRAY_SIZE(arr) calculates the number of elements by dividing the total array size by the size of one element.
- This macro can be used for arrays of any data type.
Output:
Number of elements in int array: 10 Number of elements in double array: 15
9. Common Mistake: Using sizeof with Pointer-to-Array
If you pass an array to a function, it decays to a pointer, and sizeof will return the size of the pointer rather than the array. To get the correct size, calculate it in the scope where the array is declared.
Example: sizeof with Array Decay
#include <stdio.h> void printSize(int *arr) { printf("Size of array in function: %lu bytes (incorrect)\n", sizeof(arr)); } int main() { int arr[10]; printf("Size of array in main: %lu bytes\n", sizeof(arr)); printSize(arr); return 0; }
Explanation:
- sizeof(arr) in main returns the actual size of the array.
- sizeof(arr) in printSize returns the size of the pointer because the array has decayed to a pointer.
Output:
Size of array in main: 40 bytes Size of array in function: 8 bytes (incorrect)
Summary Table of sizeof Usage
Usage | Description | Example |
---|---|---|
Primitive Data Types | Size of basic types like int, float | sizeof(int) |
Variables | Size of a specific variable | sizeof(var) |
| Expressions | Size of result of an expression | sizeof(x + y) | | Arrays | Total size of an array | sizeof(arr) | | Pointers | Size of the pointer, not pointed value | sizeof(ptr) | | Structures | Total size of a struct (including padding) | sizeof(struct) | | Dynamic Allocation | Determines memory allocation size | malloc(n * sizeof(int)) | | Macros | Used to create reusable size calculations | ARRAY_SIZE(arr) |
Complete Example: sizeof with Various Data Types
This program demonstrates sizeof with different data types, variables, arrays, pointers, and structures.
#include <stdio.h> #include <stdlib.h> struct Data { int id; double value; char label[20]; }; #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) int main() { int a = 5; double b = 10.5; int arr[10]; struct Data data; int *ptr = (int *)malloc(5 * sizeof(int)); printf("Size of int: %lu bytes\n", sizeof(int)); printf("Size of double: %lu bytes\n", sizeof(double)); printf("Size of int variable a: %lu bytes\n", sizeof(a)); printf("Size of double variable b: %lu bytes\n", sizeof(b)); printf("Size of int array: %lu bytes\n", sizeof(arr)); printf("Size of struct Data: %lu bytes\n", sizeof(data)); printf("Size of pointer ptr: %lu bytes\n", sizeof(ptr)); printf("Number of elements in array: %lu\n", ARRAY_SIZE(arr)); free(ptr); return 0; }
This comprehensive example demonstrates various sizeof applications, showing how to work with the operator across different data types and contexts.
Output:
Size of int: 4 bytes Size of double: 8 bytes Size of int variable a: 4 bytes Size of double variable b: 8 bytes Size of int array: 40 bytes Size of struct Data: 32 bytes Size of pointer ptr: 8 bytes Number of elements in array: 10
The sizeof operator is an invaluable tool in C, allowing you to calculate memory usage, manage dynamic allocation, and make code more portable.