应用于函数类型时,解释sizeof运算符的输出

har*_*del -2 c printf function sizeof

以下代码的输出是:

Hello World4

怎么样?

#include "stdio.h"  

int func()  
{
    return(printf("Hello world"));
}

int main()  
{
    printf("%d",sizeof(fun));
    return 0;  
}
Run Code Online (Sandbox Code Playgroud)

如需更多参考,请查看图片 在此输入图像描述

PS: - 据我说,sizeof()调用func()函数,其中返回statemnt调用printf函数打印hello world nd返回字符串的长度为11返回函数然后sizeof()函数返回11的大小是int和int的值取决于编译器2或4

P.P*_*.P. 5

该sizeof运营商不能在功能应用.

从C11草案,6.5.3.4 sizeof和alignof运算符

sizeof运算符不应该应用于具有函数类型或不完整类型的表达式,这种类型的带括号的名称,或者应用于指定位字段成员的表达式.

所以你正在做的是违反 C标准的约束.

编译时-std=c11,gcc会产生警告:

$gcc -std=c11 -Wall -pedantic-errors s.c
test.c: In function ‘main’:
s.c:10:23: error: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]
     printf("%d",sizeof(func));
                       ^
test.c:10:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
     printf("%d",sizeof(func));
Run Code Online (Sandbox Code Playgroud)

如您所见,还有另一个问题.你不能%d用来打印size_t.%zu是正确的打印格式说明符size_t.