1. 主页
  2. 文档
  3. C语言教程
  4. C语言运算符
  5. 用 sizeof() 写的任何东西都不会在 C 中执行

用 sizeof() 写的任何东西都不会在 C 中执行

在 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 = %d\n", a, b);
return 0;
}
输出:
a = 5, b = 4

 

 

这篇文章对您有用吗?