1

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?

chris
  • 60,560
  • 13
  • 143
  • 205
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
  • 3
    put `void printPrimeFactor(int number, int factor);` declaration before `main` – Piotr Skotnicki Sep 20 '14 at 11:46
  • 3
    Why do you still use Turbo-C ? Several compilers are [free software](http://fsf.org/) (e.g. [GCC](http://gcc.gnu.org/) ....) and are more standard conforming, gives good diagnostics, and optimize better. – Basile Starynkevitch Sep 20 '14 at 11:57
  • This line: int inputs[10]; should be int inputs[count]; so the code will have correct size array of input slots available. – user3629249 Sep 22 '14 at 05:49
  • 1
    this exact line: void printPrimeFactor(int number, int factor); (notice the semicolon at the end of the line) should be added as the very first line of your code. This is called placing the prototype for the function. Recommendations to place the subfunction first before the main function will work for this VERY SIMPLE example, but will not work in the real world. – user3629249 Sep 22 '14 at 05:50
  • BTW: Your algorithm will not work for any odd number. – user3629249 Sep 22 '14 at 06:04
  • @user3629249 I would suggest using `int inputs[100]` as 100 is the limit for `count`. Using a C99 feature like VLA in Turbo C seems to be a bit optimistic. ;) – Gerhardh Sep 10 '18 at 09:05

7 Answers7

5

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.

Dabbler
  • 9,733
  • 5
  • 41
  • 64
  • @PiotrS. The problem is that having implicitly assuming it returns an `int` it is later defined to return `void`. That is an error in any C compiler. Using Turbo-C is a problem for all sorts of other reasons; but not this one. Since ISO C99, declarations are required, but Turbo-C pre-dates that. – Clifford Sep 20 '14 at 12:21
1

You should declare printPrimeFactor() before main()

lgbo
  • 221
  • 3
  • 14
1

You need to add a function prototype if you have a function defined after the main function.

So,Add

void printPrimeFactor(int number, int factor); 

before the main function.

Also,main returns int not void.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
1

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.

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:

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.

Clifford
  • 88,407
  • 13
  • 85
  • 165
1

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

#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

#include<stdio.h>
#include<stdlib.h>
void primePrimeFactor(int number, int factor)
{

statement(s);
}
void main()
{
statement(s);
}
Vinit Dabhi
  • 993
  • 8
  • 8
0

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.

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)

So program should come like this

 #include<stdio.h>
 #include<stdlib.h>
 void printPrimeFactor(int number, int factor)
 void main()
 {
-2

This is because of a wrong header file <stdib.h>.

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
shweta
  • 1
  • 1
    I think you have to do more explaining, according to [answer]. – Yunnosch Aug 02 '21 at 08:53
  • There's no `` in OP's code. And if actually you're mentioning *your* typo: well, it's just a typo, it's not worth posting as an answer (imagine if everyone with a different typo did that...). – Eric Aya Aug 02 '21 at 09:25