What is the purpose of the ‘sizeof’ operator in C?
a) To determine the size of a variable or data type
b) To allocate memory dynamically
c) To find the length of a string
d) To initialize an array
Answer:
a) To determine the size of a variable or data type
Explanation:
The sizeof
operator in C is used to determine the size, in bytes, of a variable, data type, or array. For example, sizeof(int)
returns the number of bytes occupied by an integer. This operator is commonly used in memory management, particularly when allocating dynamic memory with functions like malloc
or calloc
.
Understanding how to use the sizeof
operator is essential for efficient memory usage and for writing portable C programs that work across different systems.