Can we call an undeclared function in C++?

1.8

Calling an undeclared function is poor style in C (See this) and illegal in C++. So is passing arguments to a function using a declaration that doesn’t list argument types:

If we save the below program in a .c file and compile it, it works without any error. But, if we save the same in a .cpp file, it doesn’t compile.

#include<stdio.h>

void f(); /* Argument list is not mentioned */

int main()
{
   f(2); /* Poor style in C, invalid in C++*/
   getchar();
   return 0;
}

void f(int x)
{ 
   printf("%d", x);
}

Source: http://www2.research.att.com/~bs/bs_faq.html#C-is-subset

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.8 Average Difficulty : 1.8/5.0
Based on 17 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.