What happens when a function is called before its declaration in C?

CServer Side ProgrammingProgramming

If we do not use some function prototypes, and the function body is declared in some section which is present after the calling statement of that function. In such a case, the compiler thinks that the default return type is an integer. But if the function returns some other type of value, it returns an error. If the return type is also an integer, then it will work fine, sometimes this may generate some warnings.

Example Code

#include<stdio.h>
main() {
   printf("The returned value: %d\n", function);
}
char function() {
   return 'T'; //return T as character
}

Output

[Error] conflicting types for 'function'
[Note] previous implicit declaration of 'function' was here

Now if the return type is an integer, then it will work.

Example Code

#include<stdio.h>
main() {
   printf("The returned value: %d\n", function());
}
int function() {
   return 86; //return an integer value
}

Output

The returned value: 86
raja
Published on 09-Apr-2019 09:22:50
Advertisements