SIZEOF

the "sizeof" operator identifies the number of bytes allocated for a given data type or variable. it is crucial for the following:

  • Memory Management: Understanding how much memory a program uses is vital for efficient allocation and avoiding errors.

  • Data Structure Design: When building arrays, structures, or unions, knowing the size of their components is essential for planning memory layout.

  • Low-Level Operations: In scenarios involving pointers, memory manipulation, or hardware interaction, sizeof helps ensure correct alignment and data access.

USAGE

this operator MUST not be used with parentheses when the argument is a literal or a value. However, if the argument is a "type" the use of parentheses is a MUST.

LITERAL/VALUE

#usage on a litral or a value
int i; char c;
i = sizeof c;

TYPE

#usage on a type
i = sizeof(char);

ARRAYS

#full array
char tab[10];
i = sizeof tab;

 * Variable i will be set to the value of 10, because this is the number of bytes occupied by the entire tab array
#
char tab[10];
i = sizeof tab[1];

 * Variable i will be set to the value of 1.
    - tab[1] returns the size of a single char element, which is 1.

Last updated