Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
sizeof() function
3.
Working of sizeof() function
4.
Exceptions
4.1.
Anything written in sizeof() is never executed
4.2.
Assignment Operator is never executed inside sizeof()
4.3.
Comparison Operator is never executed inside sizeof()
5.
Frequently Asked Questions
5.1.
What is the sizeof() function?
5.2.
What is the return type of sizeof() function?
5.3.
Is the return value of sizeof() function machine Dependent?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Anything Written in sizeof() is Never Executed

Author Nagendra
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

C++ is one of the most widely used programming languages for competitive programming. Learning C++ reinforces essential computer science principles while also providing access to a wide range of professional options.
In the programming languages C and C++, sizeof is a unary operator. It calculates the number of char-sized units required to store an expression or a data type. As a result, the construct sizeof (char) will always be 1. The blog explains the details of the execution of sizeof() function along with its exceptions.
So let's begin by having brief details about the sizeof() function.

Also See, Sum of Digits in C and C Static Function.

sizeof() function

In C/C++, the Sizeof() operator is a machine-dependent feature that changes from compiler to compiler. It's possible to call it byte-specific functionality. It aids in the allocation of variables to memory by supplying the byte and size of the variables, as well as the number they occupy. The Sizeof() method is only used to determine the precise size of a type of variable in C/C++ programming. The sizeof operator has a return type that returns the total number of bytes in memory. It is particularly beneficial in the implementation and development of portable apps because of its adaptability and flexibility.
Let's look at the working of the sizeof() function.

Click on the following link to read further: Features of C Language, Tribonacci Series

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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;
}

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;
}

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;
}

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 problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.
Do upvote our blog to help other ninjas grow. 

Happy Coding!!

Previous article
Execution of printf with ++ operators
Next article
Difference between strlen() and sizeof()
Live masterclass