+ 2
int main()
why we have to use int main() we can just use main()😬
20 Réponses
+ 5
According to C++ standards, main() must return an int value, any other return type ( including void ) is prohibited by C++ and doing so on any standard compiler would generate an error.
Although some compiler may let you get away by just omitting the return value of the main() by implicitly adding integer return type to it, but ISO C++ forbids doing so ( and some of these compilers would also warn you about the same ).
+ 3
Elahe Javid
In some languages every method or function should return something so in C or C++ we use int main() which returns integer value either 0 or 1.
int main() {
return 0;
}
if you use only main() it means you are calling that method or function.
+ 2
Kiwwi# not only parity bit (0 or 1)... return value occurs at program end because return statement exit function ^^ the value returned is usually used as the status of the program execution: 0 means all was right (success), any other value means error (failure)... each specific value could have a specific meaning related to the error occured ;)
+ 2
return value (return code) can be checked by scripts running commands...
meaning of error codes is up to the program (command) writer to define
"how they arrive to main()?"
did you meant the errors?
if you (the coder) handle errors wich could occurs in command execution: bad arguments, overflow, even unexpected errors if they are catched, and so on...
+ 2
Kiwwi# if your command takes argument and / or takes input, you can verify validity of them... if not valid, you can exit with a return code different from 0...
+ 2
if you need to dynamically allocate memory, and the allocation fail, you can also exit with a return code different ftom 0...
+ 2
We can use any default c type in main declarations, such as char main(){}, void main(){} and so on. Also, as you suggested, we can use plain main(){}, because c++ implicitly adds int return type to any function without explicit written return type. This is really helpful when we have a goal to supress our code to smaller size. But C++ standart and some compilers prohibit such declarations, so there is no reason to ignore the int main(){} declaration until there is a goal to have as little characters in code as possible.
+ 2
Calvin Thomas adding implicit int as return type to functions without a return type ( including main ) was a thing till C89 and was was removed in future revisions.
You can see this specified in Annex C of ISO/IEC 14882:1998(E) docs under section 7.1.5 rationale for which says
" In C++, implicit int creates several opportunities for ambiguity between expressions involving
function-like casts and declarations. Explicit declaration is increasingly considered to be proper style.
Liaison with WG14 (C) indicated support for (at least) deprecating implicit int in the next revision of C. "
Some compiler still support it till now for main function but they are in no way obligated to do so, that's why compilers like gcc also give you a warning whenever you try to leave main without specifying it's return type as the code might not work with any other standard compiler.
+ 1
your not mandatory to use int main() (and return an int), you could also use void main() (and avoid returning int)...
the int returned is usually used to return success state (0) or error state (any other than 0)
+ 1
also, void main() means an implicit 0 return value in any cases ;P
+ 1
Calvin Thomas the later is the function call
in C return type can be int or void
the C compiler doesn't deduct the type, you must tell him; no type inferetion in compile-time
+ 1
Arsenic I see, thank you.
0
the return value from main() [parity bit] happens at the end of the program execution, right? Why?
0
visph ty 4 answer
* what's done with such return result?
* where are defined the meanings of such errors?
* how they arrive to main()?
0
so basically it gets the error codes defined by the programmer on the "safety checks" (C that hasn't exceptions how use it?)
0
exit(code_error)
now I get, thank you!
0
Do you not get compilation errors if you don't use int main() ?
0
Martin Taylor Is there any difference between "int main()" and "main()" in C, as the latter's return type is automatically set to int by the compiler?
0
Kiwwi# But for the main function, the compiler automatically does this, right? It does so in SoloLearn.