在 C/C++ 中,sizeof() 运算符用于查找日期类型或变量的大小。用 sizeof() 编写的表达式永远不会执行。
例子:
// C program to demonstrate that the
// expressions written in sizeof() are
// never executed
#include <stdio.h>
int main(){
// The printf in sizeof is not executed
// Only the return type of printf is
// considered and its size is evaluated
// by sizeof,
int a = sizeof(printf("hey"));
printf("%d", a);
return 0;
}
输出: 4即使我们在 sizeof() 中赋值,更改也不会反映。
// One more C program to demonstrate that
// the expressions written in sizeof() are
// never executed
#include <stdio.h>
int main() {
int a = 5;
int b = sizeof(a = 6);
printf("a = %d, b = %dn", a, b);
return 0;
}
输出: a = 5, b = 4