#include <iostream>
using namespace std;
int main()
{
int a;
long b;
cout<<sizeof(a+b);
return 0;
}
The output is 8 (size of a long variable). Why doesn't it return their sum?
#include <iostream>
using namespace std;
int main()
{
int a;
long b;
cout<<sizeof(a+b);
return 0;
}
The output is 8 (size of a long variable). Why doesn't it return their sum?
the output is 8(size of a long variable)
Because of Integer Promotion.
According to cppreference.com:
the ranks of all signed integer types are different and increase with their precision: rank of signed char < rank of short < rank of int < rank of long int < rank of long long int
Therefore, a + b produces a long because a is int and b is long (rank of long is greater than int).
So, you have sizeof(long) which is 8.
why it doesn't return their addition
You may be looking for sizeof(a) + sizeof(b);
sizeof operator returns the size in bytes of a (type) or expression.
a + b is an expression and a is promoted to long after implicit conversion that's why the result is 8 on your machine i.e. the size of the long.
For calculating the total size of multiple types and/or expressions, calculate them separately and use addition like this:
auto total = sizeof(a) + sizeof(b) + sizeof(c+d);
The sizeof operator returns the size of the type you gave it, in bytes. Because you said long is 8 bytes i guess you are on x64. That being said, this is what happens:
If you have your numbers declared like this:
int a;
long b;
When you add them togheter, the expression a+b is evaluated as long type and thus sizeof(a+b) is the same as sizeof(long). If you want to see the sum of their sizes you need two sizeofs:
sizeof(a) + sizeof(b);
The sizeof operator does not evaluate the used expression as its operand. It determines the type of the expression and returns the size that an object of the type could have.
For example if you have the following code snippet
int i = 10;
std::cout << sizeof( ++i ) << '\n';
then the value of the variable i will not be changed.
From the C++ 17 Standard (5.3.3 Sizeof)
1 The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id....
To determine the type of the expression used in this sizeof operand
cout<<sizeof(a+b);
there is deduced the common type of the operands of the additive operator + using the usual arithmetic conversions.
As the rank of the type long is higher than the rank of the type int then the common type of the expression is long and the operator sizeof returns the size of an object of the type long.
Because the resulting type of an arithmetic operation is the same as the type of the operands (after their conversion 1), and being the same type, also has the same size. Addition is not a byte-concatenation operation that would result in bytes of both operands side-by-side in memory. There is no reason to expect the result type to have the total size of the operand sizes.
Consider a program adding integers in a loop. If you add a billion integers together, would you expect the result to be the size of a few gigabytes?
1 Specifically, the operands are first converted to a common type; there are no arithmetic operations on mixed types. In short, the common type is larger of the two, which in this case is long whose size on your system happens to be 8 (and before that, there would have been promotion to int, if there was a smaller operand involved).
Because of automatic type casting of int to float. When two incompatible datatypes are added the least precise data type is automatically casted to more precise data type.
In this case int has 4 bytes of memory and long has 8 bytes of memory(of course, depending on your system architecture) Thats why int is first casted as long before adding.