这段代码很好用。
#include<stdio.h>
#include<conio.h>
void show()
{
printf("Show function");
}
void main()
{
show();
}但这还需要更多。
#include<stdio.h>
#include<conio.h>
void main()
{
show();
}
void show()
{
printf("Show function");
}那就是..。我主要要声明一个“显示”函数。我为什么要这么做?
发布于 2015-05-19 05:40:34
您需要在show之前声明main,因为main在定义show之前调用了show。您可以使用如下所示的函数原型来完成此操作
#include <stdio.h>
// You reference show in main but the compiler will see main before show
// This is required to let the compiler know show will later be defined
void show();
void main()
{
show();
}
void show()
{
printf("Show function");
}发布于 2015-05-19 05:43:05
在第一个示例中,当C枚举函数调用以显示时,它已经知道函数使用了哪些参数,因为show的定义位于文件中调用的前面,而C只需遍历一次顶部到botom。在第二个例子中,何时调用显示是热的,C还没有听到任何关于一个名为show的函数的消息。
发布于 2015-05-19 05:43:10
您的代码从头到尾都是一次分析的。
第二个代码段不会编译,因为编译器不知道show ()一次命中调用的内容。
作为一个修复,您可以使用一个简单的前向声明,告诉编译器函数的名称和签名,而不直接告诉它它的主体:
#include <stdio.h>
#include <conio.h>
void show(); // Now the compiler knows the function
void main()
{
show();
}
void show()
{
printf("Show function");
}此类声明通常发生在头文件中。然后,实际的实现可以被输出到任何翻译文件中(通常也是分组的)。
https://stackoverflow.com/questions/30317503
复制相似问题