int main() or void main()

Many people still use void main() in their programs. Their justification is why should main return anything? and to where does it return a values? And my compiler accepts main() without return type.

  1. main() should return a 0 value to indicate successful execution of the program.
  2. main() returns the value to operating system.
  3. If your program writes
    •                 main() 
    •           {     /********/}
    •            it is int main(). 
    • Default return type in C is int, not void.
  4. All modern compilers give a warning when you use void main(). In fact in C++, void main() will give compiler error
Two signatures of main function are

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

First one is used in normal programs. Second signature is used when you want to access and process command line arguments.

So do not use void main() in your code, even if your compiler does not throw any errors.

 

Comments

Popular Posts