Main function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Main function

can't we use this instead of 'int' and 'return 0'? void main() { body; }

21st Jun 2017, 5:40 PM
Jay Vanraj
Jay Vanraj - avatar
3 Answers
+ 2
The main function return types and their signatures depend on the compiler that you are using. int main is the standard and for good reason. The return value tells the OS whether or not there was a problem with the execution of the program and it closed with/without errors. A number other than 0 would indicate that there was an issue with the program. The only compiler that I can think of off the top of my head that allows a void return type is the Microsoft one used with Visual Studio. Some compilers will also allow the absence of the return 0 statement in the main function only, and will implicitly return 0 for you. However, the return type used in the declaration of the main function itself must still be int. Generally speaking the most common main function signatures in C++ are: main() and main(int argc, char* argv[]) This second signature is used to take arguments from the command line. The first parameter (argc) is used for the count of the arguments passed to the program and the second parameter (argv) is an array of the argument values passed in to the program.
21st Jun 2017, 6:23 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Yes, we can, but using int and return 0 is better couse in programming when programme return 0, mean that program work without mistakes. it is like saying "good morning" when you meet friend, it is not the rule, but we usually do it.
21st Jun 2017, 6:03 PM
★RussianBias★
★RussianBias★ - avatar
+ 1
wow, I try it in this compilator, and it show mistake, but my compilator don't show it.
21st Jun 2017, 6:07 PM
★RussianBias★
★RussianBias★ - avatar