C语言 函数应该有原型错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25948390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Function should have a prototype error
提问by Harikrishnan
I have written the following C program:
我编写了以下 C 程序:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int count;
scanf("%d",&count);
if(count < 1 || count > 100)
{
exit(1);
}
int inputs[10];
for(int i = 0; i < count; i++)
{
scanf("%d",&inputs[i]);
if(inputs[i] < 1 || inputs[i] > 30000)
{
exit(1);
}
}
for(i = 0; i < count; i++)
{
printPrimeFactor(inputs[i], 2);
printf("\n");
}
}
void printPrimeFactor(int number, int factor)
{
if(number % factor == 0)
{
int flag = 1, newNumber;
newNumber = number;
for(int i = 0; i < factor/2; i++)
{
if(factor % i == 0)
{
flag = 0;
break;
}
}
if (flag)
{
printf("%d ", factor);
newNumber = number / factor;
}
factor++;
if(factor <= newNumber / factor)
{
printPrimeFactor(newNumber, factor);
}
}
}
And on compiling(in windows, turbo C), I am keeping getting the error:
在编译(在 Windows 中,turbo C 中)时,我不断收到错误消息:
Function 'printPrimeFactor' should have a prototype error
I couldn't find any problem with the code. What can be the issue?
我找不到代码有什么问题。可能是什么问题?
回答by Dabbler
Your function is called before the compiler has seen its definition, so the compiler is saying "I want to see this function's prototype first".
你的函数在编译器看到它的定义之前被调用,所以编译器说“我想先看看这个函数的原型”。
This means you put void printPrimeFactor(int number, int factor);before the function call.
这意味着你把它放在void printPrimeFactor(int number, int factor);函数调用之前。
Alternately, you can place the entire function (i.e. its definition) before the call.
或者,您可以将整个函数(即它的定义)放在调用之前。
回答by Spikatrix
You need to add a function prototype if you have a function defined after the mainfunction.
如果在函数之后定义了函数,则需要添加函数原型main。
So,Add
所以,添加
void printPrimeFactor(int number, int factor);
before the mainfunction.
在main函数之前。
Also,mainreturns intnot void.
此外,main返回intnot void。
回答by Clifford
When the compiler encounters the call to printPrimeFactor()in main(), it has not yet seen the definition, so it is required to assume that it is a function returning an int, and the number and types of the parameters are inferred from the arguments passed to the call when first encountered.
当编译器遇到对printPrimeFactor()in的调用时main(),还没有看到定义,所以需要假设它是一个返回 an 的函数int,并且参数的数量和类型是从第一次传递给调用的参数中推断出来的遭遇。
In this case, printPrimeFactor()does not return an int. When the definition is encountered the compiler detects the difference between the definition signature and the inferred declaration. The solution is to define-before-use or add an explicit prototype declaration before use. So for example before main()add:
在这种情况下,printPrimeFactor()不返回int. 当遇到定义时,编译器会检测定义签名和推断声明之间的差异。解决方案是在使用前定义或添加显式原型声明。例如,在main()添加之前:
void printPrimeFactor(int number, int factor) ;
ANSI C89 and ISO C90 allow implicit declarations, C99 and C++ do not. There really are very few reasons why you should use an antique 16 bit compiler. Not least because neither the IDE, compiler nor the code it generates it will not even run on Win64.
ANSI C89 和 ISO C90 允许隐式声明,C99 和 C++ 不允许。您应该使用古老的 16 位编译器的原因确实很少。尤其是因为 IDE、编译器和它生成的代码都不能在 Win64 上运行。
回答by lgbo
You should declare printPrimeFactor() before main()
您应该在 main() 之前声明 printPrimeFactor()
回答by Vinit Dabhi
Function should have a prototype error solve there are 2 ways as per my knowledge:- 1)if u define your function name before the main() it's called function declaration like
函数应该有一个原型错误,据我所知,有两种方法可以解决:- 1)如果你在 main() 之前定义了你的函数名,它被称为函数声明
#include<stdio.h>
#include<stdlib.h>
void prinPrimeFactor(int number, int factor);
void main()
and
和
2)if you use your total function before the main() then it's work like
2)如果你在 main() 之前使用你的总函数,那么它的工作原理就像
#include<stdio.h>
#include<stdlib.h>
void primePrimeFactor(int number, int factor)
{
statement(s);
}
void main()
{
statement(s);
}
回答by Anurag AV
The compiler wants to see all the functions that is in the program before it is been called. So you either write the whole function before main or you give the function's prototype before main and write the function after main.
编译器希望在调用之前查看程序中的所有函数。因此,您要么在 main 之前编写整个函数,要么在 main 之前给出函数的原型,然后在 main 之后编写函数。
So in the above program the function "void printPrimeFactor(int number, int factor)" is after main so you will have to give the prototype of the function. prototype of the function is nothing but the whole name of the function, i.e
所以在上面的程序中,函数 "void printPrimeFactor(int number, int factor)" 在 main 之后,所以你必须给出函数的原型。函数的原型就是函数的全名,即
void printPrimeFactor(int number, int factor)
So program should come like this
所以程序应该是这样的
#include<stdio.h>
#include<stdlib.h>
void printPrimeFactor(int number, int factor)
void main()
{

