前面的章节中讲述过 sizeof 接受的参数可以是对象也可以是表达式,但是 sizeof(expression) 在运行时不会对接受的表达式进行计算,编译器只会推导表达式的类型从而计算占用的字节大小;
C++
#include <iostream>
using namespace std;
int main(int argc, char * argv[])
{
int x = 4;
sizeof(x++);
printf("%d\n", x);
return 0;
}
C
#include<stdio.h>
void main(){
printf("%d\n", sizeof(1==1));
}
/*
运行结果:
4
*/
由于 C++ 语言有 bool 类型,布尔型占 1 个字节,因此下面的程序返回 1;
C++
#include <iostream>
using namespace std;
int main() {
cout << sizeof(1==1) << endl;
return 0;
}
/*
1
*/
阅读量:2021
点赞量:0
收藏量:0