3 ms·
int main() { return 0; } Main takes argc and argv, but it's idiomatic to omit them if unused.
by mkehrt 7y ago
int main() { return 0; }
Main takes argc and argv, but it's idiomatic to omit them if unused.
- shawxe 7y agoWhy not just use int main(void)?
- deleted 7y ago[deleted]
- mkehrt 7y agoI'm confused. main doesn't take void--it takes two arguments. The f() syntax means that the definition doesn't specify how many arguments the function will be called with, while f(void) means it takes no arguments.
- pksadiq 7y agoAs per the standard, main() can take no argument [ie, main(void)] or 2 arguments [main (int, char asterisk asterisk) or equivalent] or some implementation defined manner. See 5.1.2.2.1 in C2x working draft[0]. [0] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2346.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2346.pdf
- tropo 7y agoWow, it is painful to see that they still haven't accepted case ranges. They are supported by gcc, clang, and icc. Like so: case 123 ... 456: Switching on strings is another thing people have been wanting for half a century. It would seep up many programs, because most programmers don't bother to generate a perfect hash or a carefully-balanced tree of "if".
- eridius 7y agoThis is irrelevant; this is the function definition, which means the compiler knows this function takes zero arguments. It works because with the way the C ABI works on all platforms I'm aware of, extra arguments passed to the function can be completely ignored by the function with no ill effects, so the fact that _start actually invokes main with argc and argv can be ignored by main. Which is to say, you could also write this as int main(void) { return 0; } and that would work equally as well.
- innocenat 7y agoNot on stdcall i.e. Win32 API.
- eridius 7y agoI've never done Windows programming but AFAICT you don't use `int main()` on Win32 anyway, you write something like int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR, int) (where WINAPI is just a macro for __stdcall) I also found a reference saying you can in fact use `int main()` if you want to, in a GUI subsystem Windows app, by using the Microsoft linker options /subsystem:windows /ENTRY:mainCRTStartup, but in that case you're not writing a stdcall function anymore. EDIT: I guess I should clarify, my previous comment was talking about cdecl functions, i.e. the default calling convention in C.
- Vogtinator 7y agoThat's a special case though and unrelated to (). Like it's also explicitly allowed to omit a return statement in main altogether, which implies a return 0.
- kazinator 7y agoThat was added in C99; previously, "falling off" the end of main without a return statement resulted in an indeterminate termination status. Countless sloppily written C programs had garbage termination statuses because of this, making it impossible for higher level programs (e.g. scripts) to reliably test for successful execution.