When to use 'void main ()' and when to use 'int main ()' in C language ? Will anyone help me by posting examples, please. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 26

When to use 'void main ()' and when to use 'int main ()' in C language ? Will anyone help me by posting examples, please.

I have seen that somewhere 'void main ()' is used and somewhere 'int main ()' with 'return 0'. In some compilers I also have found that after using 'int main ()' and then not using 'return 0' gives the correct output. Why ?

1st Aug 2017, 5:21 PM
Tanweer Ashif
Tanweer Ashif - avatar
7 Answers
+ 8
@Martin Thanks Martin for helping me in such an awesome way. Happy friendship day Martin.
6th Aug 2017, 7:11 PM
Tanweer Ashif
Tanweer Ashif - avatar
+ 5
Always use int main(). void main() is just acceptable on some compilers.
1st Aug 2017, 5:29 PM
Rrestoring faith
Rrestoring faith - avatar
+ 5
Nice quote: " 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. " Source: https://stackoverflow.com/questions/18928279/how-does-int-main-and-void-main-work
1st Aug 2017, 7:13 PM
Bogdan Sass
Bogdan Sass - avatar
+ 3
@Martin - only int main() is standard C (and it has been like that from ANSI C89). void main() was kept around only for backwards compatibility, but you should avoid using it.
1st Aug 2017, 7:12 PM
Bogdan Sass
Bogdan Sass - avatar
+ 2
void main is for Turbo C++, and it's better not to use it.
6th Aug 2017, 7:49 AM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
+ 1
"void main ()" is also used with the Arduino IDE rather than "int main ()".
6th Aug 2017, 6:37 PM
Benjamin Nixon
Benjamin Nixon - avatar
0
void main is used when you want that your function should not return any value. However, if you want the function to return an int value, we use int main
6th Aug 2017, 10:45 AM
Prarabdh Garg
Prarabdh Garg - avatar