Search code examples
cfunctionfunction-prototypes

function prototypes in main function?


As i understand we can not declare a function inside another function. But we can call one function in another function.

In main function we we usually call functions like this:

int abc(int some)
{
  return x;
}

int main()
{
  int x = 10;
  abc(x); //calling function abc inside main function.
  return 0;
}

but today while looking at a sample code i saw something like this:

int main()
{
  int abc(int x); // which compiled fine 
}

which works, but only thing I am trying to understand here is what is use of such statements?.

  1. We can not define a function inside main function.
  2. We can call functions inside main function.
  3. but this one looks like more of a function prototype which is also declared outside all functions at the start of c program files.

Solution

  • It's a matter of scope. You can declare the prototype of a function inside the main. The thing is that it will only be able to be called inside the main, and nowhere else. And the actual code of the function will be outside of the main method.

    So in your second example, where int abc(int x); is inside the main, the function abc will only be able to be called inside the main function.