1

What is the beneficial effects and difference between using main() function as

  • void main()

    or

  • int main(void)

arshajii
  • 127,459
  • 24
  • 238
  • 287
Aman Singh
  • 743
  • 1
  • 10
  • 20
  • you should always use int main(), because upon finishing the application, the OS need to know the exit status of the application. – aah134 Jul 13 '13 at 18:45

3 Answers3

3

A main function is called by a pre-load library that runs upon execution by the operating system. Many operating systems handle the result of an executable - and declaring main to return a type of int means that the main function can return a value. Obviously declaring main to return a type of void means that the main function cannot return a value - and some compilers will not permit this depending on what the target architecture of your build is.

The difference between declaring a function () or (void) is documented many times elsewhere on StackOverflow.

Community
  • 1
  • 1
PP.
  • 10,764
  • 7
  • 45
  • 59
  • isn't it a just for the standardization of C and C++ that many compilers wouldn't allow void main() ? – Aman Singh Jul 13 '13 at 18:51
  • @AmanSingh I've used void main in my past. But that's a long past. I can't remember `gcc` letting me use void main in recent years. – PP. Jul 13 '13 at 19:10
0

if you want your application to have the ability to return a value, you must use

int main(void).

Winks
  • 580
  • 1
  • 7
  • 17
-2

First is wrong, second is correct. End of story.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85