how does int main() and void main() work

user2106271 picture user2106271 · Sep 21, 2013 · Viewed 100.9k times · Source

I am a beginner in 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 happeneing when we use void main() and what is happening when i use int main() in simple language and so on.
I know but cant 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 simple hello world 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?

Answer

Carl Norum picture Carl Norum · Sep 21, 2013

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.