+ 2

Difference b/w "int main ()" and "void main()"

difference b/w "int main ()" and "void main()" or it is same I'm confused in it

30th Aug 2017, 6:07 PM
Boo
Boo - avatar
6 Réponses
+ 2
"void main()" is illegal C++! (Your compiler will know what you meant and ignore it though)
30th Aug 2017, 6:14 PM
Schindlabua
Schindlabua - avatar
+ 9
Words from the creator himself. http://www.stroustrup.com/bs_faq2.html#void-main
31st Aug 2017, 1:13 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
In C++, the main returns a 0 or 1 (exit codes for success/failure), and that's why you utilize int main for that purpose. That exit code is often utilized by the OS to generate an error if needed. If you use void, it doesn't return a value at all, and thus wouldn't return an exit code for success/failure status. However, as Schi pointed out, void main() is illegal and has never been a part of C/C++ languages.
30th Aug 2017, 7:53 PM
AgentSmith
+ 3
you mean void main () is illegal
30th Aug 2017, 6:31 PM
Boo
Boo - avatar
+ 2
thx
30th Aug 2017, 6:16 PM
Boo
Boo - avatar
+ 1
void is used when you don't need to return any values from function. int is used to return a int value from function. i.e. int sum( int a, int b ){ return a+b; } void sayHello(){ cout<<"Hello!"; } char, float and double can also be used in any user-defined function in place of int depending on what type of value you want to return. void main(){} returns no values int main(){} returns 0 which is int data type you cannot use char, float and double for main() function
31st Aug 2017, 3:48 PM
Kartikey Sahu
Kartikey Sahu - avatar