+ 2

int main()

why we have to use int main() we can just use main()😬

4th Jul 2021, 8:25 PM
faran allah verdi
20 Answers
+ 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 ).
5th Jul 2021, 6:20 AM
Arsenic
Arsenic - avatar
+ 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.
4th Jul 2021, 8:33 PM
A͢J
A͢J - avatar
+ 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 ;)
4th Jul 2021, 10:35 PM
visph
visph - avatar
+ 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...
4th Jul 2021, 10:46 PM
visph
visph - avatar
+ 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...
4th Jul 2021, 10:53 PM
visph
visph - avatar
+ 2
if you need to dynamically allocate memory, and the allocation fail, you can also exit with a return code different ftom 0...
4th Jul 2021, 10:54 PM
visph
visph - avatar
+ 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.
5th Jul 2021, 10:02 AM
Ретр0
+ 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.
6th Jul 2021, 8:52 AM
Arsenic
Arsenic - avatar
+ 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)
4th Jul 2021, 9:04 PM
visph
visph - avatar
+ 1
also, void main() means an implicit 0 return value in any cases ;P
4th Jul 2021, 10:47 PM
visph
visph - avatar
+ 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
6th Jul 2021, 7:26 AM
Kiwwi#
Kiwwi# - avatar
+ 1
Arsenic I see, thank you.
6th Jul 2021, 9:07 AM
Calvin Thomas
Calvin Thomas - avatar
0
the return value from main() [parity bit] happens at the end of the program execution, right? Why?
4th Jul 2021, 10:30 PM
Kiwwi#
Kiwwi# - avatar
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()?
4th Jul 2021, 10:41 PM
Kiwwi#
Kiwwi# - avatar
0
so basically it gets the error codes defined by the programmer on the "safety checks" (C that hasn't exceptions how use it?)
4th Jul 2021, 10:50 PM
Kiwwi#
Kiwwi# - avatar
0
exit(code_error) now I get, thank you!
4th Jul 2021, 10:54 PM
Kiwwi#
Kiwwi# - avatar
0
Do you not get compilation errors if you don't use int main() ?
5th Jul 2021, 10:19 PM
Sonic
Sonic - avatar
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?
6th Jul 2021, 6:39 AM
Calvin Thomas
Calvin Thomas - avatar
0
Kiwwi# But for the main function, the compiler automatically does this, right? It does so in SoloLearn.
6th Jul 2021, 8:17 AM
Calvin Thomas
Calvin Thomas - avatar