#include <iostream>
using namespace std;
int main()
{
int a;
long b;
cout<<sizeof(a+b);
return 0;
}
输出为8(长变量的大小)。为什么不返回它们的总和?
最佳答案
the output is 8(size of a long variable)
由于整型促销。
根据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
因此,
a + b
会生成long
,因为a
是int
,而b
是long
(long的秩大于int)。因此,您具有
sizeof(long)
,即 8 。why it doesn't return their addition
您可能正在寻找
sizeof(a) + sizeof(b);
关于c++ - 为什么在sizeof()中添加两个变量类型会返回最大的变量大小值而不是它们的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60968536/