0
Why we are not using void main for declaring a function? Instead we are using such as int?
3 odpowiedzi
+ 5
thx
that was really useful information!
+ 3
Main always returns a value. Even if you don't specifically declare it to return a value, most compilers will pretty much assume you meant to put "return 0;" at the end of your main.
We use an integer return type for main so that we are able to diagnose whether our program ran successfuly, or if it ended in a way we didn't expect, in which case we return a value other than 0 (usually 1). Helpful in larger programs with diagnosing problems.
Always have the return type for main as int; it is the C++ standard, and many compilers may not even let you put any other return type.
+ 1
any function can return a value and main also.
but the usage is different.
you may want to use main return value when you call it within a process, like a BATCH(.bat) file in windows or shell file in *nix.
assume your program as a function call in a batch file and getting the return value of you program through main in batch file,
so you could understand if your program exited (returned) with the conditions you expected.