main() function in C

95,015

Solution 1

  1. A declaration of a function is needed only before a function is used. The definition is itself a declaration, so no prior prototype is required. (Some compilers and other tools may warn if a function is defined without a prior prototype. This is intended as a helpful guideline, not a rule of the C language.)
  2. Because the C standard says so. Operating systems pass the return value to the calling program (usually the shell). Some compilers will accept void main, but this is a non-standard extension (it usually means "always return zero to the OS").
  3. By convention, a non-zero return value signals that an error occurred. Shell scripts and other programs can use this to find out if your program terminated successfully.

Solution 2

1) All functions must be declared in their function prototype, and later on, in their definition. Why don't we have to declare the main() function in a prototype first?

Not true. Simple example:

void foo(){}  //definition

int main()
{
    foo();
    return 0;
}

Only when one function is called but the definition isn't seen yet, a declaration is required. That will never happen to main since it is the starup of the program.


2) Why do we have to use int main() instead of void main()?

Because the standard says so. (To be more precise, it's true on a hosted environment, which is usually the case)

C99 5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.


3) What does return 0 exactly do in the main() function? What would happen if I wrote a program ending the main() function with return 1, for example?

The return value indicates the result of the program. Usually 0 indicates success while other values indicates different kinds of failure.

Solution 3

You are not free to chose the return type of main because you did not write the code that calls main. The code calling main already existed before you even thought about learning C. It was written by the folks providing the C runtime startup code, which usually is linked automatically to your executable without you knowing. This code often resides in a file called crt0.o (created from crt0.c or even assembler in crt0.s). It expects to use a return value indicating success (0) or failure (nonzero), plus possibly other information like whether the code was terminated due to a signal and if so, which one. These are bits of Unix history, that I won't repeat here :-)

Solution 4

1) Not necessarily; a definition also serves as a declaration. Secondly, there are only a few valid signatures for main anyway, and you normally won't call main within your code unless you're writing an entry for the IOCCC.

2) Short answer: because the language definition says so. Longer answer: this is how your program indicates success or failure to the host environment. An individual implementation is free to support additional signatures for main, but it must document those additional signatures. If your compiler documentation does not list void main() as a legal signature, then you shouldn't use it.

3) By convention (at least on *nix systems where C was first used), a status of 0 indicates success, and a non-zero status indicates ... something other than success. Exactly what value corresponds to what status is up to the implementation.

Share:
95,015

Related videos on Youtube

Heikki
Author by

Heikki

Updated on July 09, 2022

Comments

  • Heikki
    Heikki almost 2 years

    I've been learning C programming in a self-taught fashion for some weeks, and there are some questions that I have concerning the main() function.

    1. All functions must be declared in their function prototype, and later on, in their defintion. Why don't we have to declare the main() function in a prototype first?

    2. Why do we have to use int main() instead of void main()?

    3. What does return 0 exactly do in the main() function? What would happen if I wrote a program ending the main() function with return 1;, for example?

    • huysentruitw
      huysentruitw almost 11 years
      main() even has its own Wikipedia page
    • aragaer
      aragaer almost 11 years
      1) is wrong - prototype isn't "a must"
    • Prashant Kumar
      Prashant Kumar almost 11 years
      @user2718426 Heads up, 'round these parts, we like to keep each question to only one question at a time. So your post sounds like three questions. Though you'd find lots of writing about the second already, like stackoverflow.com/questions/9356510/int-main-vs-void-main-in‌​-c
    • Michael M.
      Michael M. almost 11 years
      You can get the value returned by your program with $? on unix systems (must be something else on Windows) : ./myprog && echo $?
    • Matteo Italia
      Matteo Italia almost 11 years
      @Michael: on Windows with cmd.exe it's %errorlevel% IIRC.
    • Michael M.
      Michael M. almost 11 years
      @MatteoItalia good to know ! Thanks.
    • Fred Foo
      Fred Foo almost 11 years
      @Michael If you use && in that construct, echo won't run unless $? is zero.
    • Veltas
      Veltas almost 11 years
      The funny thing is, on most systems the vast majority of numbers that main() can return aren't actually returned properly because the OS only supports a subset (like 0 - 255 or something silly). I usually just restrict myself to 0 and 1, or maybe a few more.
    • nIcE cOw
      nIcE cOw almost 11 years
      Instead of returning 0, from the main, it would be wise, if you use the predefined macro for the same, from stdlib.h, which is return EXIT_SUCCESS;, since this will expand to a system-dependent integral expression :-)
  • Eric Postpischil
    Eric Postpischil over 5 years
    There is no rule in the C standard (versions from 1999 to 2018 at least) that says main must not be called from any other function. Clause 5.1.2.2.3 even includes a reference to “the initial call to the main function,” thus acknowledging the possibility of subsequent calls. Nor is there any rule that if any function, main or otherwise, must not be called from any other function for some reason, it must not be declared.
  • Eric Postpischil
    Eric Postpischil over 5 years
    Possibly the statement in the C standard that “The implementation declares no prototype for this function [main]” could cause some confusion, but it just means that the C implementation does not provide a declaration. That is, it is not built into the compiler or any standard headers. It does not mean the program may not declare a prototype for main.