0

I understand why C requires a main function to begin the execution of a program, but of the now three books I've read entirely or in portion, none has explained why programs begin by declaring main as int, or with an argument of void:

int main(void)

can someone tell me what the purpose of this is?

4 Answers4

2

The return value of main() is used to indicate success or failure to its parent process. More generally, it can be used to communicate back specific statuses as well, though C doesn't define those.

If main() returns 0 or EXIT_SUCCESS, then the program was successful. EXIT_FAILURE or non-zero, then it failed.

The void in the parameter list simply says that it takes no arguments. This is because of a (mis)feature of C which allows you to declare a function without fully specifying the paramters it takes. A function declared int func(); can be called with any number of parameters, but int func(void); can only becalled with zero.

Example

on linux,

two trivial programs:

$ cat ret0.c
int main (void) { return 0; }
$ cat ret42.c
int main (void) { return 42; }

Then in `bash` we can look at
$ ./ret0 ; echo $?
0
$ ./ret42 ; echo $?
42

So it's possible to use that status when calling your program.

FatalError
  • 52,695
  • 14
  • 99
  • 116
1

The int return is there to give an error indicator back to the OS. return 0 means no error, all other codes (typically return 1) indicates the program could not finish successfully. Other programs (e.g., shell scripts) can use this error code to determine if your program executed its task, or ran into a problem.

void just means no arguments. It's the same as

int main()
{
    /* program */
}

but more explicit.

A program can take command line arguments, in which case main must be defined as

    int main(int argc /* number of arguments */, char *argv[] /* arguments)
    {
        /* program
    }

Any good book on C should explain this.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • I thought C was very type specific, wouldn't declaring int main restrict the type of data I can work with? – Andres Meza Sep 26 '14 at 17:39
  • @AndresMeza: I don't get what you mean. The return value from `main` is *only* an error indicator. The program can use `` facilities to do any input/output it likes. – Fred Foo Sep 26 '14 at 17:40
  • ok I understand, thank you all for your answers – Andres Meza Sep 26 '14 at 17:42
  • Note that this only applies to what are called *hosted* implementations (basically, anything running on top of an operating system). *Freestanding* implementations (i.e., embedded applications) may use a different entry point, and it may or may not return `int`. – John Bode Sep 26 '14 at 17:44
  • Doesn't even need to be embedded. `main()` doesn't even need to be declared under newer VC++ so that one can allow for command line parameters being Unicode. So VC++ now has `_tmain` and `_wmain` entry points as well as `main()`. Which gets called (and what the arguments are) will be determined by compiler options. – Michael Petch Sep 26 '14 at 18:18
  • @JohnBode : Am I correct while saying that user space programs are hosted applications , where as programs like device drivers are freestanding ? ( w.r.t C language ) – Abhishek Choubey Sep 26 '14 at 20:04
  • People, I really appreciate all your comments, but must every C novice be bothered with all permitted non-standard extensions when they're only asking what the default means? – Fred Foo Sep 26 '14 at 20:59
1

First off let us forget about main. In C(not C++) if you define a function with no parameters like this

int f(){ return 0;}

It is legal to call such a function with any number of arguments:

int a = f(); /* legal */
int a = f("help", 1, 2.0); /* legal */

If you want your function f to only work with exactly no arguments you can amend it like this:

int f(void){return 0;}
int a = f(); /* legal */
int a = f("help", 1, 2.0); /* not legal as it has too many parameters */

The same thing applies to main() and main(void) . In most cases in the reasonable world most people would never care however I have encountered legal code that calls main within the program.

So declaring main like:

int main() {
     /* Do a bunch of stuff here */
}

Allows for code elsewhere in your program to do this:

main(); 
main(1,2,3,4);

By declaring main(void) you add a compiler check that prevents the latter example main(1,2,3,4) from compiling.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • If we have the prototype as int main(), and make a call like main(1,2,3,4) then what kind of problems we might run into ? What I think is, since we dont have any formal parameters to hold the values passed as arguments to main, there should be no problem. Am I correct ? – Abhishek Choubey Sep 26 '14 at 20:26
  • 1
    Since in 'C' the caller pushes things on the stack and is responsible for cleaning the stack up after calling a function any number of parameters can be put on the stack and the function being called remains oblivious. You can find out more about this calling convention (aka `cdecl`) [here](http://en.wikipedia.org/wiki/X86_calling_conventions#cdecl) One advantage to this is that it allows functions like printf to take arbitrary number of arguments without getting messed up. – Michael Petch Sep 26 '14 at 21:41
1

Like any other function in C, main is also a function. Thus it has a return type and can
accept arguments.

int  main(void)      

Here int is the return type of main. Many people still use 'void' because they do not
update themselves with the current language standards. haccks's answer mentions about
the latest standard specifying the signature of main function.
Its general and good practice to have int as main's return type as it tells the parent process of main about the termination (success / failure) of the program.

Like any other function main is also capable of accepting arguments, but with an exception, i.e. the arguments to main are given before the execution of program starts. These are called "command line arguments".
main can accept arguments in two ways :

1. int main(void)
         or
   int main()

2. int main(int argc, char *argv[])
         or
int main(int argc, char **argv) 

The first one says that main is not expecting any arguments where as the second declara-
-tion expects the user to provide command line arguments to main.

Note : main should take either 0 or 2 arguments. If you try to give any number of
arguments other than these then it gives the following warning when you compile your code

warning: ‘main’ takes only zero or two arguments [-Wmain]

Edit : The above warning is displayed if you are using gcc.

Abhishek Choubey
  • 883
  • 6
  • 16
  • ".. **it** gives the following warning .." -- well, good compilers do. The behavior you describe seems specific for `gcc`. – Jongware Sep 26 '14 at 21:07