Home  >  Article  >  Backend Development  >  In C language, what happens if a function is called before it is declared?

In C language, what happens if a function is called before it is declared?

WBOY
WBOYforward
2023-08-27 19:21:061094browse

In C language, what happens if a function is called before it is declared?

If we do not use some function prototypes, and the function body is declared in a certain part after the statement that calls the function. In this case, the compiler assumes that the default return type is integer. But if the function returns a value of another type, an error will be returned. This works fine if the return type is also an integer, it may sometimes generate some warnings.

Sample Code

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

Output

[Error] conflicting types for &#39;function&#39;
[Note] previous implicit declaration of &#39;function&#39; was here

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

Sample code

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

Output

The returned value: 86

The above is the detailed content of In C language, what happens if a function is called before it is declared?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete