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


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
", 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
", function()); } int function() {    return 86; //return an integer value }

Output

The returned value: 86

Updated on: 30-Jul-2019

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements