【发布时间】:2021-10-17 01:24:46
【问题描述】:
简单的问题:
为什么我的代码看起来像这样编译:
#include <unistd.h>
#include <stdio.h>
void hello()
{
write (1, "3", 3);
}
int main ()
{
hello();
}
输出:3
当我在write (1, "3", 3); 之后的行中添加return(0); 时,为什么它无法编译?
我得到的错误信息是:
2function.c:7:2: error: void function 'hello' should not return a value [-Wreturn-type]
return(0);
谢谢!
【问题讨论】:
-
void()函数不返回值。编译器错误很明显。 -
如果您放弃
<unistd.h>并使用printf("3");输出,您的程序更多可移植到具有C 编译器的所有环境(Windows、在线编译器、智能冰箱、Lunar着陆器,克林贡太空船,...);不仅仅是具有 POSIX 接口的环境。
标签: c compiler-errors return return-value function-definition