Detailed Explanation of sizeof() Operator in C Language

In C language, the sizeof() operator is widely used. It is used to determine the number of storage units occupied by an expression or a specified data type, measured in the size of a char. The sizeof() operator includes one operand, which can be an expression or a type conversion, where the conversion is placing the data type in parentheses. The operand can be not only primitive data types like integers or floating-point data types but also pointer data types and composite data types like unions and structures.

The need for the sizeof() operator mainly arises because the program needs to know the storage size of primitive data types. Although the storage size of data types is constant, it can vary across different platforms. For example, we can dynamically allocate array space using the sizeof() operator:
int *ptr = malloc(10 * sizeof(int));

In the above example, we use the sizeof() operator, which applies to the type conversion of int. We use the malloc() function to allocate memory and return a pointer to the allocated memory. The size of the memory space is equal to the number of bytes occupied by the int data type multiplied by 10.

Note: The output may vary on different machines, for example, it will display different outputs on a 32-bit operating system compared to a 64-bit operating system for the same data type.

The sizeof() operator behaves differently based on the type of the operand.
  • The operand is a data type

  • The operand is an expression

👇 Click to Claim 👇
👉 C Language Knowledge Resource Collection

When the Operand is a Data Type

#include <stdio.h>
int main() {  int x = 89; // Variable declaration  printf("The size of variable x is: %d\n", sizeof(x)); // Display the size of variable x  printf("The size of integer data type is: %d\n", sizeof(int)); // Display the size of integer data type  printf("The size of character data type is: %d\n", sizeof(char)); // Display the size of character data type  printf("The size of floating-point data type is: %d\n", sizeof(float)); // Display the size of floating-point data type  return 0;}

Output:

The size of variable x is: 4The size of integer data type is: 4The size of character data type is: 1The size of floating-point data type is: 4

In the above code, we use sizeof() to determine the size of variable x and various data types (such as int, char, and float).

When the Operand is an Expression

#include <stdio.h>
int main() {  double i = 78.0;  float j = 6.78;  printf("The size of (i + j) expression is: %d\n", sizeof(i + j)); // Display the size of expression (i + j)  return 0;}

Output:

The size of (i+j) expression is : 8

In the above code, we created two variables i and j, which are of double and float types respectively, and then printed the size of the expression using sizeof(i + j).

Output

size of (i+j) expression is : 8

Detailed Explanation of sizeof() Operator in C LanguageProgrammer Technical Exchange GroupDetailed Explanation of sizeof() Operator in C Language

Scan the code to join the group, remember to note: city, nickname, and technical direction.

Leave a Comment