The sizeof Operator in the Computer Language, C
Part 12 of Complete C Course
Foreword: The sizeof Operator in the Computer Language, C
By: Chrysanthus Date Published: 3 Jun 2024
Introduction
sizeof ( type-name )
sizeof unary-expression
Yes, the operator is "sizeof" and not a mathematical symbol or some other symbol, on the keyboard. If the operand is a type-name (type of value), such as char, int, or float then the operand is in parentheses. If the operand is an identifier, or a number, then the parentheses are optional. So, the size of operator returns the number of bytes (object-width), an object of its operand type, would occupy in memory.
The output of the following code segment is 1:
int myInt = sizeof(char);
printf("%i\n", myInt);
Note that "char" is in parentheses. The output of the following code segment is 4:
int myInt = sizeof(int);
printf("%i\n", myInt);
The output of the following code segment is 4:
int myInt = sizeof(float);
printf("%i\n", myInt);
The output of the following code segment is 8:
int myInt = sizeof(double);
printf("%i\n", myInt);
Note that all the type-name operands are in parentheses. The following code segments show the use of the sizeof operator without parentheses:
char ch;
int myInt = sizeof ch;
printf("%i\n", myInt);
1 is returned. The number of bytes of the type-name (type of value) identified by ch has been returned.
int myInt = sizeof 6;
printf("%i\n", myInt);
4 is returned, because 6 is an integer.
float flt;
int myInt = sizeof flt;
printf("%i\n", myInt);
4 is returned. The number of bytes of the type-name (type of value) identified by flt has been returned.
int myInt = sizeof 2.5;
printf("%i\n", myInt);
8 is returned. This means that it considers a number literal with a decimal point, as a double type (not as a float).
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The size in bytes of an array, structure or union, can be returned by the sizeof operator - see later.