Harikrishnan T Harikrishnan T - 1 month ago 7
C Question

Function should have a prototype error

I have written the following C program:

#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:

Function 'printPrimeFactor' should have a prototype error


I couldn't find any problem with the code. What can be the issue?

Answer

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.

Alternately, you can place the entire function (i.e. its definition) before the call.