In C, if a function is called before its declaration, the compiler assumes return type of the function as int.
For example, the following program fails in compilation.
if the function char fun() in above code is defined before main() then it will compile and run perfectly.
for example, the following program will run properly.
The following program compiles and run fine because function is defined before main().
What about parameters? compiler assumes nothing about parameters. Therefore, the compiler will not be able to perform compile-time checking of argument types and arity when the function is applied to some arguments. This can cause problems. For example, the following program compiled fine in GCC and produced garbage value as output.
There is this misconception that the compiler assumes input parameters also int. Had compiler assumed input parameters int, the above program would have failed in compilation.
It is always recommended to declare a function before its use so that we don’t see any surprises when the program is run (See this for more details).
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- When is copy constructor called?
- When are Constructors Called?
- What happens when more restrictive access is given to a derived class method in C++?
- What happens if loop till Maximum of Signed and Unsigned in C/C++?
- what happens when you don't free memory after using malloc()
- What is the difference between single quoted and double quoted declaration of char array?
- C | Variable Declaration and Scope | Question 1
- C | Variable Declaration and Scope | Question 2
- C | Variable Declaration and Scope | Question 3
- C | Variable Declaration and Scope | Question 4
- C | Variable Declaration and Scope | Question 6
- C | Variable Declaration and Scope | Question 7
- C | Variable Declaration and Scope | Question 8
- Functions that are executed before and after main() in C
- unordered_multiset and its uses
- C++ bitset and its application
- C++ string class and its applications
- C++ String Class and its Applications | Set 2
- isspace() in C/C++ and its application to count whitespace characters
- Macros and its types in C/C++
Improved By : aanuj0110