In my code, why is the absence of a function declaration not a problem for one function, but gives a warning for another?

In the next program, I use the two functions prd () and display (). I did not declare any of them before main () before calling them in main (), and I defined it as after main (). while prd () runs smoothly inside main (), the calling display () shows the warning "The previous implicit declaration" display "was here." What is different from display (), that there is a warning for it, but not for other prd () functionality? I have not announced any of them to start with. If there is a warning due to calling one, but the other works fine.

#include<stdio.h> int main() { int x=8,y=11; printf("The product of %d & %d is %d",x,y,prd(x,y)); display(); return 0; } int prd(int x,int y) { return x*y; } void display() { printf("\n Good Morning"); } 

PS: And I would really appreciate it if you could answer this secondary question: "Is a function declaration not necessary at all in C if there is a definition for it?". I have the habit of declaring all program functions before the main () function, and then defining them after the main () function. Am I mistaken?

+6
source share
3 answers

When you use uneclared display() , the compiler implicitly declares it as if it were returning an int .

When the compiler finally sees the definition of display() , it sees that the return type is void , but it has already assumed that it is int , and therefore the definition and implicit declaration are different, hence an error / warning.

+9
source

The error occurs because C considers all unused functions with an int return type. Your display function is later defined with a void return type.

Changing the return type of display() to int removes the warning.

+4
source

By default, the compiler accepts undeclared functions as returning an int .

This is true for your prd function, but it does not match display() like its void . This causes the compiler to raise a warning.

During the 2nd, it is always suitable for declaring functions.

+2
source

All Articles