C The sizeof Operator
1. What is sizeof?
-
sizeofis a compile-time operator in C. -
It returns the size (in bytes) of a data type, variable, array, or structure.
-
Syntax:
-
The result is usually of type
size_t(an unsigned integer type).
2. Using sizeof with Data Types
Output (typical on 32-bit/64-bit system):
%zuis used to printsize_ttype returned bysizeof.
3. Using sizeof with Variables
4. Using sizeof with Arrays
-
For arrays,
sizeofgives the total memory used. -
You can calculate the number of elements using:
Example:
Output:
Here, each
intis 4 bytes, so 10 × 4 = 40 bytes.
5. Using sizeof with Pointers
-
Pointer size is independent of data type it points to.
-
Typically 4 bytes on 32-bit and 8 bytes on 64-bit systems.
6. Key Points
-
sizeofis evaluated at compile time. -
Can be used with data types, variables, arrays, and structures.
-
Useful for memory calculations, dynamic allocation, and array sizing.
-
Returns size in bytes.
