17

I am a beginner in the C language. Can anyone explain in detail using example how main(), int main(), void main(), main(void), void main(void), int main(void) work in C language?

As in, what is happening when we use void main() and what is happening when I use int main() in simple language and so on?

I know, but I can’t understand what is it doing:

  1. main() - function has no arguments
  2. int main() - function returns int value
  3. void main() - function returns nothing, etc.

When I write a simple Hello, World! program using the int main() return 0, it still gives me the same output as when using void main()), so how does it work? What is its application?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
user2106271
  • 179
  • 1
  • 1
  • 3
  • 6
    `void main()` is useful mainly as an indication that you're using a textbook written by someone who doesn't know the C language very well. Of the forms you listed, only `int main(void)` is valid. (It's not *quite* that simple, but that's close enough for now.) – Keith Thompson Sep 21 '13 at 05:08

2 Answers2

26

Neither main() or void main() are standard C. The former is allowed as it has an implicit int return value, making it the same as int main(). The purpose of main's return value is to return an exit status to the operating system.

In standard C, the only valid signatures for main are:

int main(void)

and

int main(int argc, char **argv)

The form you're using: int main() is an old style declaration that indicates main takes an unspecified number of arguments. Don't use it - choose one of those above.

Carl Norum
  • 201,810
  • 27
  • 390
  • 454
  • 1
    In resume, they are just allowed for compatibility backwards with old C code written when operating system didn't required an exit status. You always should to return an integer value in `main` – Jack Sep 21 '13 at 02:51
  • 3
    @Jack: `void main()` is not for backwards compatibility with anything. The same standard that first introduced the `void` keyword defined the return type of `main` as `int`. – Keith Thompson Sep 21 '13 at 05:03
  • int main(int argc, const char **argv, const char **env) – dns Dec 25 '13 at 16:43
  • @dns, not in standard C. The implementation can define whatever it likes, though. – Carl Norum Dec 26 '13 at 01:04
15

If you really want to understand ANSI C 89, I need to correct you in one thing; In ANSI C 89 the difference between the following functions:

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

is:

int main()

  • a function that expects unknown number of arguments of unknown types. Returns an integer representing the application software status.

int main(void)

  • a function that expects no arguments. Returns an integer representing the application software status.

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

  • a function that expects argc number of arguments and argv[] arguments. Returns an integer representing the application software status.

About when using each of the functions

int main(void)

  • you need to use this function when your program needs no initial parameters to run/ load (parameters received from the OS - out of the program it self).

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

  • you need to use this function when your program needs initial parameters to load (parameters received from the OS - out of the program it self).

About void main()

In ANSI C 89, when using void main and compiling the project AS -ansi -pedantic (in Ubuntu, e.g) you will receive a warning indicating that your main function is of type void and not of type int, but you will be able to run the project. Most C developers tend to use int main() on all of its variants, though void main() will also compile.

L. F.
  • 16,219
  • 7
  • 33
  • 67
Ron Nuni
  • 1,505
  • 1
  • 17
  • 14