1

I see different people writing different statements for declaring main()

Like,

int main()

void main()

int main(void)

int main(int argc, char* argv[])

I mean when to pass arguments? what is the need of it? when to declare int as return type and when void?

I have just started practising coding when I see people writing different main() declaration I actually get puzzled.

hrishikesh
  • 346
  • 2
  • 17
  • 2
    You forgot to add `int main(int argc, char* argv[])` – mikeyq6 Nov 30 '15 at 17:09
  • @mikeyq6 That's probably the most important one of them all, too :) – Sergey Kalinichenko Nov 30 '15 at 17:09
  • 1
    Possibly a dup of this: [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – P.P Nov 30 '15 at 17:10
  • and to add to the OP confusion, let's not forget about `char* envp[]` as a third argument to main (see http://stackoverflow.com/questions/10321435/is-char-envp-as-a-third-argument-to-main-portable)...and yes I know that is is non-portable and highly implementation defined, and I've never seen it used in 20 years of programming. It's just one of those days where I felt the need to stir the pot, as it were, a bit :) . – thurizas Nov 30 '15 at 17:28

5 Answers5

3

You need input arguments if you want your program to get some data from the command line. For example, you can run smth like this in the Command Prompt

./test arg1 arg2

In you have int main( int argc, char* argv[] )declared you'll get argc = 3 and argv is an array of chars representing each element {test, arg1, arg2}.

If you don't want you program to get any user console input just declare it as int main( ).

Think of main as an entry point for your program, the point from where execution takes place. On some embedded platforms you can name your entry point whatever you like (i.e. int superPuperMain() ).

The return code of main is treated when you launch your program from some script and you are interested in the return code of it. Think of your program as a function with return code here. Of course it's not correct but it gives you a general feeling about it :).

Dmitry
  • 1,472
  • 15
  • 25
2

int main() means you will end your program with return 0;(or other value,0 is standard for "everything's fine").

  • void main() will return nothing.
  • int main(void) means there is no arguments.
  • int main(int argc,char *argv[]) means there are two parameter, one is int type and another one is char type pointer array. The first parameter stores the number of command line arguments entered and the second parameter is used to store the arguments.
mch
  • 8,070
  • 2
  • 24
  • 39
Saikat Kundu
  • 350
  • 1
  • 12
0

From the C Standard:

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;10) or in some other implementation-defined manner.

So to answer your question, the first and third forms you wrote are the same (writing void as the parameter list is a vestige from Kernigan and Richie's C that prevents unspecified parameter list on declarations. In those days empty parameter lists permitted arbitrary argument lists on call sites).

The second one is implementation defined, and you missed the last one in which you specify parameters (argc and argv) which are populated with the arguments supplied when the program is executed.

imreal
  • 9,700
  • 2
  • 28
  • 45
0

this "main style" is depending to compiler that you using. Every program returning exit code on program exit. (it's 0 code when the program was exited with no errors), so it is int. (but in case of main - void means the same ;) ) The arguments are passing optionally, for example: - you run yourprogram.exe with some arguments from command line and now you could read this arguments.

0

The main function is defined in _start(on POSIX/UNIX Operating Systems). The compiler looks for the _start symbol and then jumps to it. _start then calls main. The reason main is written in so many ways is that the call to main is very vague. I'll address these and several more.
Let's start with main()
This only occurs when main is defined in a header and its type is already set.
Now let's address int main()
this is when you want a return value so the OS or shell can detect if an error has occurred,for example make looks to see if -1 is returned and then stops executing if it has been returned.
void main() is used when you don't want to have a return value,usually a simple program.
int main(void) is the same as int main just written in a different format.
now let's address arguments.
There is another way main can be written and that is: int main(int argc,char *argv[])
this is used when you are looking for command line arguments to be passed to the function. argc is the number of arguments(including the command) and argv[] are the arguments.For example if you run ./a.out argc will be 1 and argv will be [0]a.out. If you run ./a.out foo argc will be 2 and argv will be [0]a.out,[1]foo.

Zach S
  • 76
  • 7
  • `_start()` is an implementation detail, and it's POSIX-specific. The entry point in Windows binaries has its name vary between subsystem and character width, the most "basic" one being `_mainCRTStartup()`. – Medinoc Nov 30 '15 at 17:20
  • Setting the return type of the `main` function to `void` is non-standard. It is implementation-defined. The only standard forms are `int main(void)` and `int main(int argc, char *argv[])` or equivalents such as using different parameter names, or using `char **argv` instead of `char *argv[]` – Ian Abbott Nov 30 '15 at 18:04