Piy*_*shM 1 c return function void c89
注意:我在代码块IDE上使用Windows 7和gcc编译器.
我发现如果函数的返回类型为void,则可以使用所有形式的return语句
例如:
void message() //If i skip this declaration still it works
main()
{
message();
}
void message()
{
return 5; //also return 5.0 and return a(some variable) also works
}
Run Code Online (Sandbox Code Playgroud)
因为,void指的是'什么都没有被返回',那么我们如何在这里使用return语句.这是否意味着所有程序都返回无论是无效还是其他返回类型?
我有一个关于void返回类型的查询:
如果我使用这个程序
main()
{
printf("%d",message());
}
void message()
{
}
Run Code Online (Sandbox Code Playgroud)
它输出为1,
并且不会给出错误
但如果我使用它,我会收到错误:
void message(); //when I use the declaration
main()
{
printf("%d",message());
}
void message()
{
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?