Working of sizeof() function
The sizeof() operator is a versatile and adaptable operator for computing bytes and memory for consuming and returning the required values after processing. It isn't compiler-specific; therefore, it differs from one compiler to the next. It compiles any unary data type and then uses the size of its operand to compute. It returns the variable's size. If a machine is 32 bits, memory allocation and other computations will work in the same way, and the output will vary accordingly. If the machine is 64 bits, the output will vary accordingly.
If you supply an argument to the sizeof operator as an expression, it will parse the full regular expression and then produce an output with the same size as the expression. Similarly, for the other parameters, such as type and variable-name, it will accept the parameter type and then refer to the data type, which can be int, char, or float, to be considered for the function. The same is true for the variable name, and the value of the variables can also be computed. When a variable is assigned as an array or a linked list, the sizeof () operator is used to effortlessly calculate the number of bytes required.
For better understanding let us look at this example.
Consider the code snippet:
#include <stdio.h>
int main(){
int x=sizeof(50); //Assign the value of size of Integer to x
int y= sizeof(50.5); //Assign the value of size of Double to y
printf("The size of Integer is %d \n The size of double is %d", x,y);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
The size of Integer is 4
The size of double is 8
Explanation:
In the code snippet, x is assigned with the value of Integer, and y is assigned the value of Double. Hence based on the compiler, the output is 4 and 8, respectively.
Exceptions
There are some exceptions in the sizeof() operator, let's understand it in details:
Anything written in sizeof() is never executed
Lets understand this topic from a small example.
Consider the following code Snippet:
#include <stdio.h>
int main(){
int x = sizeof(printf("Ninja")); //The printf function will not work.
printf("The size is %d", x);
return 0;
}
Output:
The size is 4
Explanation:
In the above code snippet, the printf function will not work; rather, the return type of printf is Integer, so it prints the size of Integer, i.e., 4.
You can also read about the jump statement and Short int in C Programming
Assignment Operator is never executed inside sizeof()
The assignment operator inside the sizeof() function is never executed. For better understanding lets look at the example.
Consider the following code snippet:
#include <stdio.h>
int main(){
int a=9;
int x = sizeof(a=6); //The Assignment operator will not work.
printf("The size is %d \n", x);
printf("The value of a is %d", a);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
The size is 4
The value of a is 9
Explanation:
In the above code snippet, we are trying to assign the value 6 to the variable a, but the sizeof operator restricts it. Hence the value of a is printed as 9.
Comparison Operator is never executed inside sizeof()
Consider the following code snippet:
#include <stdio.h>
int main(){
int a=9;
int x = sizeof(a>=6); //The Comparison operator will not work.
printf("The size is %d \n", x);
printf("The value of a is %d", a);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
The size is 4
The value of a is 9
Explanation:
In the above code snippet, we are trying to compare the value of variable a with 6, but the sizeof() operator restricts it. Hence the value of a is printed as 9.
Frequently Asked Questions
What is the sizeof() function?
The sizeof() method is used to determine the precise size of a type of variable in C/C++ programming.
What is the return type of sizeof() function?
The sizeof operator has a return type that returns the total number of bytes in memory.
Is the return value of sizeof() function machine Dependent?
In C/C++, the sizeof() operator is a machine-dependent feature that changes from compiler to compiler.
Conclusion
In this article, we have extensively discussed the execution of the sizeof() function. The article explains that anything written in sizeof() is never executed.
We hope that this blog has helped you enhance your knowledge regarding sizeof() function and if you would like to learn more, check out our articles on C++. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. To practice and improve yourself in the interview, you can check out Top 100 SQL problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews.
Do upvote our blog to help other ninjas grow.
Happy Coding!!