【发布时间】:2018-11-20 16:07:38
【问题描述】:
我实际上是在 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...");
}
当我运行它时,它会返回如下错误消息。
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();
^~~~~~~
当我将消息函数放在main() 上方时,它没有显示错误。为什么?我们不能把函数放在main() 之后吗?这里的隐式声明是什么?
【问题讨论】:
-
你需要在
main之前声明函数:void message();。 -
你需要在使用之前对你的函数进行 decalre。只需在 main
void message();之前放一个原型即可 -
main的正确声明是int main (void)和int main (int argc, char **argv)(您将看到用等效的char *argv[]编写)。 注意:main是type int的函数,它返回一个值。请参阅:C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570)。