stellar
stellar

Reputation: 53

How does main() function compare to other functions in C?

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?

Upvotes: 2

Views: 2223

Answers (5)

Steve Summit
Steve Summit

Reputation: 47942

How does main() function compare to other functions in C?

Saying that main is "treated the same as every function" is somewhat incomplete. The main function is mostly like any other function in C, but it does have one key distinction, and two key differences.

Distinction: main is special in that it is the entry point: the one function that is automatically called by the execution environment to get your program up and running.

Difference #1: Unlike every other function in C, main has two signatures. It can either be int main(void) (that is, accepting no arguments), or int main (int, char **).

Difference #2: If you write a main function without a return statement, it is compiled as if you had written return 0; at the end.

An ordinary function, by contrast, must have exactly one signature, agreed on once and for all by the function definition, all function declarations, and all callers. In an ordinary function, if it's declared as returning anything other than void, and if control flow "falls off the end" without an explicit return statement, you're heading into undefined behavior territory.

Upvotes: 0

Osman GOKTURK
Osman GOKTURK

Reputation: 1

I have thought of this question in general. my starting point was "where I should handle dynamic allocation i.e malloc? in the main or the library function?

I lastly figured out that, if we do not distinguish between the main and other functions, then it is not a question. So, it is up to the taste of programmer to divide into seperate functions or not.

However, talking about differences between main vs other functions by the example of malloc-free stuff, we allocate memory in the function but we free it in the main. This is might be obvious ofcourse!. if we use free in the function before return, what we are returning right!

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310970

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.

Upvotes: 3

Deduplicator
Deduplicator

Reputation: 45654

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.

Upvotes: 5

Barmar
Barmar

Reputation: 780929

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.

Upvotes: 6

Related Questions