The sizeof
operator is used to determine the size of a variable or data type in bytes.
Syntax:
Example:
int x = 5; size_t size_of_x = sizeof(x); printf(“The size of x is %zu bytes\n”, size_of_x);
Here, we use sizeof
to determine the size of the integer variable x
, and store the result in the size_of_x
variable. We then print the size of x
using printf()
.
Benefits:
Using sizeof
can help with memory management, as it allows us to allocate the correct amount of memory for variables and data types. It can also help with understanding the layout of data structures in memory.
Usage:
Determine the size of a variable:
Determine the size of a data type:
Determine the size of an array:
Determine the size of a struct: