We mainly use the Sizeof() Operator in C for giving the total amount of storage required (in bytes), for storing any object in the form of an operand. The sizeof() operator allows a user to avoid the specification of the machine-dependent type of data sizes in any program.
In this article, we will take a look at the sizeof() operators in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to learn more.
Table of Contents
- Why Do We Use the sizeof() Operator in C?
- The Requirement of sizeof() Operator in C Programs
- If the Operand in a Program is a Data Type
- If the Operand in a Program is an Expression
- Practice Problems on sizeof() Operator in C
- FAQs
Why Do We Use the sizeof() Operator in C?
We generally use the sizeof() operator in the C language so that we can determine the sizes of the data types or the expressions that are specified in the storage units of char-size. The sizeof() operator consists of just one operand that can either be a cast data type or an expression. Here, the cast refers to a data type that is enclosed within the parenthesis in the program. Keep in mind that these data types can be primitive, like floating (float) and integer (int), or it can be compound data types, like structures (struct) and union, or it can be a pointer data type as well.
The Requirement of sizeof() Operator in C Programs
All programs are mainly aware of the size (storage size) of all the primitive forms of data types. Even though the data type’s storage size is pretty constant throughout, it can easily vary when we implement it on various different platforms. For instance, we can perform the allocation of the array space dynamically with the use of the sizeof() operator.
int *ptr= malloc(20*sizeof(int));
In the example given above, we are using the sizeof() operator, and we have applied it to the int cast type. Also, we have used the malloc() function here for the allocation of the memory. It would, in turn, return the pointer pointing towards this memory (allocated memory) in the program. The space of the memory in the program is equivalent to the total bytes that the int data type occupies, multiplied by 20 (as specified in the code).
Important Point to be Noted – The output obtained due to the sizeof() operator varies a lot on the basis of different types of machines. For instance, a 32-bit OS (operating system) and a 64-bit OS (operating system) would generate very different results/outputs for the very same data type in a program.
The behavior of the sizeof() operator varies a lot according to what type of operand is available with us in the program, such as:
- If the Operand in a program is a Data Type
- If the Operand in a program is an Expression
If the Operand in a Program is a Data Type
#include <stdio.h>
int main()
{
int q=89; // declaration of the variable
printf(“the total size of the program’s variable q is equal to %d”, sizeof(q)); // the program displays the total size of the variable q
printf(“\nthe total size of the program’s integer data type is equal to %d”,sizeof(int)); // the program displays the total size of the integer data type
printf(“\nthe total size of the program’s character data type is equal to %d”,sizeof(char)); // the program displays the total size of character data type
printf(“\nthe total size of the program’s floating data type is equal to %d”,sizeof(float)); // the program displays the total size of the floating data type
return 0;
}
Here, in the code mentioned above, we are trying to print the total size of various data types, like char, int, and float using the sizeof() operator.
The Output obtained from the above-mentioned code will be:
the total size of the program’s variable q is equal to 4
the total size of the program’s integer data type is equal to 4
the total size of the program’s character data type is equal to 1
the total size of the program’s floating data type is equal to 4
If the Operand in a Program is an Expression
#include <stdio.h>
int main()
{
double x=78.0; // initialization of variable
float y=6.78; // initialization of variable
printf(“The total size of the (x+y) expression is equal to : %d”,sizeof(x+y)); // the program displays the actual size of the given expression (x+y).
return 0;
}
In the case of the code mentioned above, we performed the creation of the two main variables, namely x and y. These are of double type and float type respectively. After this, the program prints the expression’s size with the use of the sizeof(x+y) operator.
The Output obtained out of the program given here would be:
The total size of the (x+y) expression is equal to : 8
Practice Problems on sizeof() Operator in C
1. What would be the output obtained out of the program mentioned below?
#include <stdio.h>
int main() {
char k = ‘X’;
double m = 4.65;
printf(“The total size of the variable k is equal to : %d\n”,sizeof(k));
printf(“The total size of an expression is equal to : %d\n”,sizeof(k+m));
int x = (int)(k+m);
printf(“The total size of the explicitly converted expression is equal to : %d\n”,sizeof(x));
return 0;
}
A. The total size of the variable k is equal to : 1
The total size of an expression is equal to : 8
The total size of the explicitly converted expression is equal to : 4
B. The total size of the variable k is equal to : 4
The total size of an expression is equal to : 8
The total size of the explicitly converted expression is equal to : 5
C. The total size of the variable k is equal to : 4
The total size of an expression is equal to : 2
The total size of the explicitly converted expression is equal to : 5
D. The total size of the variable k is equal to : 1
The total size of an expression is equal to : 2
The total size of the explicitly converted expression is equal to : 4
Answer: A. The total size of the variable k is equal to : 1
The total size of an expression is equal to : 8
The total size of the explicitly converted expression is equal to : 4
2. What would be the output obtained out of the program mentioned below?
#include <stdio.h>
int main() {
int u = 16;
printf(“The total size of the variable u is equal to : %d\n”,sizeof(u));
printf(“The total size of the int data type is equal to : %d\n”,sizeof(int));
printf(“The total size of char data type is equal to : %d\n”,sizeof(char));
printf(“The total size of the float data type is equal to : %d\n”,sizeof(float));
printf(“The total size of the double data type is equal to : %d\n”,sizeof(double));
return 0;
}
A. The total size of the variable u is equal to : 4
The total size of the int data type is equal to : 1
The total size of char data type is equal to : 4
The total size of the float data type is equal to : 1
The total size of the double data type is equal to : 8
B. The total size of the variable u is equal to : 4
The total size of the int data type is equal to : 8
The total size of char data type is equal to : 2
The total size of the float data type is equal to : 1
The total size of the double data type is equal to : 4
C. The total size of the variable u is equal to : 4
The total size of the int data type is equal to : 4
The total size of char data type is equal to : 1
The total size of the float data type is equal to : 4
The total size of the double data type is equal to : 8
D. The total size of the variable u is equal to : 2
The total size of the int data type is equal to : 4
The total size of char data type is equal to : 1
The total size of the float data type is equal to : 4
The total size of the double data type is equal to : 8
Answer: C. The total size of the variable u is equal to : 4
The total size of the int data type is equal to : 4
The total size of char data type is equal to : 1
The total size of the float data type is equal to : 4
The total size of the double data type is equal to : 8
3. What would be the output obtained out of the program mentioned below?
#include<stdio.h>
int main() {
float floatType;
int intType;
char charType;
double doubleType;
printf(“The total size of the float is equal to : %zu bytes\n”, sizeof(floatType));
printf(“The total size of the int is equal to : %zu bytes\n”, sizeof(intType));
printf(“The total size of char is equal to : %zu byte\n”, sizeof(charType));
printf(“The total size of the double is equal to : %zu bytes\n”, sizeof(doubleType));
return 0;
}
A. The total size of the float is equal to : 4 bytes
The total size of the int is equal to : 4 bytes
The total size of char is equal to : 2 bytes
The total size of the double is equal to : 8 bytes
B. The total size of the float is equal to : 4 bytes
The total size of the int is equal to : 2 bytes
The total size of char is equal to : 4 bytes
The total size of the double is equal to : 8 bytes
C. The total size of the float is equal to : 4 bytes
The total size of the int is equal to : 4 bytes
The total size of char is equal to : 1 byte
The total size of the double is equal to : 4 bytes
D. The total size of the float is equal to : 4 bytes
The total size of the int is equal to equal to : 4 bytes
The total size of char is equal to equal to : 1 byte
The total size of the double is equal to equal to : 8 bytes
Answer: D. The total size of the float is equal to : 4 bytes
The total size of the int is equal to : 4 bytes
The total size of char is equal to : 1 byte
The total size of the double is equal to : 8 bytes
Frequently Asked Questions
Why do we use the sizeof() operator in the C language?
We mainly use this operator for giving the total amount of storage required (in bytes), for storing any object in the form of an operand. The sizeof() operator allows a user to avoid the specification of the machine-dependent type of data sizes in any program.
We generally use the sizeof() operator in the C language so that we can determine the total size of the data type or the expression that is specified in the storage units of char-size. The sizeof() operator consists of just one operand that can either be a cast data type or an expression. Here, the cast refers to a data type that is enclosed within the parenthesis in the program. Keep in mind that these data types can either be primitive like floating (float) and integer (int), or it can be compound data types, like structures (struct) and union, or it can be a pointer data type as well.
Can a program be aware of the total size of the data types used in the code if we don’t use the sizeof() operator in the program?
All programs are mainly aware of the size (storage size) of all the primitive forms of data types. Even though the data type’s storage size is pretty constant throughout, it can easily vary when we implement it on various different platforms. For instance, we can perform the allocation of the array space dynamically with the use of the sizeof() operator.
int *ptr= malloc(20*sizeof(int));
In the example given above, we are using the sizeof() operator, and we have applied it to the int cast type. Also, we have used the malloc() function here for the allocation of the memory. It would, in turn, return the pointer pointing towards this memory (allocated memory) in the program. The space of the memory in the program is equivalent to the total bytes that the int data type occupies, multiplied by 20 (as specified in the code).
Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.
Also Explore,
Comments