我实际上是在Ubuntu 18.04上使用C语言.我不使用任何IDE.
#include <stdio.h>
void main()
{
message();
printf("\nCry, and you stop the monotomy!\n");
}
void message()
{
printf("\nSmile, and the worldsmiles with you...");
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它返回错误消息,如下所示.
msg.c: In function ‘main’:
msg.c:5:2: warning: implicit declaration of function ‘message’ [-Wimplicit-function-declaration]
message();
^~~~~~~
msg.c: At top level:
msg.c:8:6: warning: conflicting types for ‘message’
void message()
^~~~~~~
msg.c:5:2: note: previous implicit declaration of ‘message’ was here
message();
^~~~~~~
Run Code Online (Sandbox Code Playgroud)
当我把消息函数放在上面时,main()它显示没有错误.为什么?我们不能把功能放到后面main()吗?什么是隐含声明?
main如果你愿意,你可以把功能放在后面; 如果你在main定义它们之前调用它们,你应该在之前声明它们main:
void message();
void main()
...
Run Code Online (Sandbox Code Playgroud)
如果没有这个,编译器会假定这message是一个外部链接函数返回int,然后当它遇到它的实际定义时,message会抱怨冲突类型message(因为它已经决定了消息返回int,而不是void).