[Question] Reason for void() and main() [Solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

[Question] Reason for void() and main() [Solved]

So in c++ ,we use void() and main() in several incidents...so I just wanna know the actual reason for including it..and when do we includ it..

24th Jan 2022, 1:59 PM
•Đคяк รтяεคм 𝅘𝅥𝅮 ᴺ•ᵀ•ᴬˢᵘʳᵃᵖᵖᵘˡᶦ
•Đคяк รтяεคм 𝅘𝅥𝅮 ᴺ•ᵀ•ᴬˢᵘʳᵃᵖᵖᵘˡᶦ - avatar
3 Answers
+ 3
ROOKIE Runtime Terror Ipang Thank u all👍
26th Jan 2022, 1:57 PM
•Đคяк รтяεคм 𝅘𝅥𝅮 ᴺ•ᵀ•ᴬˢᵘʳᵃᵖᵖᵘˡᶦ
•Đคяк รтяεคм 𝅘𝅥𝅮 ᴺ•ᵀ•ᴬˢᵘʳᵃᵖᵖᵘˡᶦ - avatar
+ 1
When our program is simple, and it is not going to terminate before reaching the last line of the code, or the code is error free, then we can use the void main(). But if we want to terminate the program using exit() method, then we have to return some integer values (zero or non-zero). In that situation, the void main() will not work. So it is good practice to use int main() over the void main(). For more refer:- https://www.tutorialspoint.com/difference-between-void-main-and-int-main-in-c-cplusplus#:~:text=When%20our%20program%20is%20simple,zero%20or%20non%2Dzero). https://stackoverflow.com/questions/636829/difference-between-void-main-and-int-main-in-c-c
24th Jan 2022, 2:43 PM
saurabh
saurabh - avatar
+ 1
C++ standard recommendation is to use `int` as return type for main function. https://en.cppreference.com/w/cpp/language/main_function
24th Jan 2022, 2:50 PM
Ipang