2
votes

The documentation is:

In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts.

But when I run

int main() {
    printf("%d", square(3));
    return 0;
}

int square(int n) {
    int sq = n * n;
    return sq;
}

the program prints 9. So does the main() function get executed only after all the other functions or is it special in a different way?

3
main is called first, and is calling other functions. So eventually the called functions terminate before main terminates. - Eugene Sh.
What "all the other functions"? The only function that is called automatically is main. - melpomene
It's misleading to say that main is called "first". ONLY main is ever called by the OS. All other functions are never called unless they are called by main(), or by some function called by main(), etc. - Lee Daniel Crocker
I'm uncertain from where that quotation was drawn (certainly not the C language standard), but it seems reasonably accurate, and it seems also to address the question. So what is it you really want to know, and / or what about the behavior you observe makes you question the quoted description? - John Bollinger
This question makes a lot more sense if you imagine OP being Python user introduced to compiled languages for the first time - Mad Physicist

3 Answers

6
votes

The order is this:

  1. OS calls main()
  2. main() calls square(3).
  3. square(3) calculates the result 9 and returns it.
  4. main() calls printf("%d", 9)
  5. printf() prints 9 on the terminal and returns the number of characters printed (1).
  6. main() returns 0 to the OS.
5
votes

There is a difference between getting defined and getting called.

main() is the one function directly called by the runtime to execute the program. Some others might be called in initializers for global variables, or some other special circumstances, but let's ignore those, especially as they are irrelevant to your case.

And all the others get called someway from there, directly or indirectly. Or they are simply dead code.

There is another difference for main() since C99: return 0; is implicit for it.

As an aside, ramp up the warning level, all those functions called before / without being declared are errors.

3
votes

This code

int main() {
    printf("%d", square(3));
    return 0;
}

int square(int n) {
    int sq = n * n;
    return sq;
}

relies on the obsolescent feature of implicit function declarations, because there is no declaration of the name square before its usage in main. This feature was removed from the C standard in its 1999 revision. All the most commonly used C compilers still honor it (with warnings) for backward compatibility's sake, but actually using it is bad style and can hide bugs. You should write this program with an explicit "forward declaration" of square above main:

int square(int n);

int main(void)
{
/* remainder of program as you have it */

(Not putting anything at all between the argument parentheses of a function declaration or definition is also an obsolescent feature. To declare or define a function that takes no arguments you have to say (void).)

(In C, for historical reasons, preferred style is to put the opening curly brace of a function definition on its own line, even if all other opening braces are "cuddled" onto the same line as the if, for, etc.)

Having said that, the function square is executed because, in main, it is called:

int main() {
    printf("%d", square(3));
                 ^^^^^^
    return 0;
}

That is, first main gets control and then inside main the function square is called. If main did not call square, or call a function that calls it, square would never be executed.

The difference between the function main and any other function is that in a host environment the function main contains the entry point to the program that is it gets the control first. And the function main may be defined without the return statement though its return type is int.