6 ms·
Void main(void) - the Wrong Thing
- gnosis 16y agoAlso see: http://jk-technology.com/ctips01.html#int_main http://jk-technology.com/ctips01.html#int_main and http://www.eskimo.com/~scs/readings/voidmain.960823.html http://www.eskimo.com/~scs/readings/voidmain.960823.html
- beej71 16y agoBesides, int main(void) is shorter to type. And, horridly, return isn't necessary in main, so you don't even have to be bothered to type that. int main(void)--for the lazy AND correct!
- wnoise 16y ago> And, horridly, return isn't necessary in main, Only true for C++.
- hoop 16y agobut it works for me!
- henning 16y agoWorks On My Machine(tm) http://www.codinghorror.com/blog/2007/03/the-works-on-my-machine-certification-program.html http://www.codinghorror.com/blog/2007/03/the-works-on-my-mac...
- beej71 16y agoAlso true in C99 5.1.2.2.3: "reaching the } that terminates the main function returns a value of 0."
- ssp 16y agoFor the even lazier: main(void) Though I vaguely recall something about this being deprecated in C99.
- JoachimSchipper 16y agoYes, that's deprecated. And it was horrible before.
- zokier 16y agoWouldn't int main() be even better. Shorter, and acknowledges that main actually takes parameters (command line arguments).
- hackermom 16y agoGCC (as an example) turns () and ( void ) into the exact same output. I have some memory of long, long ago entering the world of C in the hands of the DICE compiler for the Amiga, that being clear in the declaration of functions is The Right Way: say void, or say inputs. Semantically I consider ( void ) to simply say "in this case, we're not going to be using any input vectors", rather than "this function can never have inputs; it's not supported". Clarity is never wrong.
- zokier 16y agomain is a special case, but generally () and (void) are different, void meaning that the function cannot have any parameters and empty allowing any number of arguments. Therefore imho is clearer to use main() rather than main(void), indicating that there may be parameters, but we don't care. http://codepad.org/UGLG7yRm http://codepad.org/UGLG7yRm and http://codepad.org/w2GyTkB1 http://codepad.org/w2GyTkB1
- guylhem 16y agoFor the last recruit tests I gave, it was the easiest way to discard applicants without proper C knowledge. A wrong answer predicted a bad score on pratical (and lenghty) tests like code commenting, finding bugs, etc. Anecdote: When corrected, a guy who allegedly coded in C for 10 years even asked what was the use for main to return something :-(( So we talked about unix return values, $? in bash and he had a spark in his eye. I had a tear in mine.
- nerd_in_rage 16y agoseriously? i knew that shit when i was 15. taught myself C on an amiga 500.
- m0nastic 16y agoI remember in high school my C teacher made us write it that way, even after most of the kids in the class tried to convince him against it. In his defense, he didn't know C (he was taking night classes at a local college and then teaching us what he had been taught the night before).
- i386 16y agoYour learnt C in highschool? Thats pretty great. In highschool we had a very talented and enthusiastic teacher who rewrite the vb6 examples in quite a bit of the terms curriculum in Python and introduced us to that first.
- m0nastic 16y agoI actually predominantly learned COBOL and RPG in High School (which makes me feel old, even though this was in 97). I thought COBOL was useless at the time, but amusingly have had to do two separate source code audits of COBOL code in the past year (where I was the only person in my company who could do it). My senior year, they decided to pilot a C programming class. None of the teachers knew C, but one Math teacher volunteered to take night classes and teach us what he was learning. I would say in about two months, every student in the class was significantly more knowledgeable than the teacher; but he was very cool about it. The only bad part was he spent about four months on graphics programming; which wouldn't have been bad, except we learned Borland C Graphics (getpixel, putpixel, etc.) which were pretty much useless. The year culminated in us sending a team to a C Programming Contest being held at a nearby university. I always thought it was amusing that the grand prize (which we didn't win) was a copy of Visual Cafe (the Java IDE).
- brownleej 16y agoI see void being passed as a parameter list to main in a lot of examples. All of the things I've read suggest that the only difference between having a void parameter list and an empty parameter list is in function declarations, and that the two are equivalent in function definitions. Why is it used so frequently in the main function definition, if it serves no purpose?
- deleted 16y ago[deleted]
- pmjordan 16y agoYou've probably been reading C++ material. Only in C++ int f(); and int f(void); are identical. In C, however, int f(); Declares a function taking any number of arguments. In C++, that would be denoted as int f(...); Which is unfortunately a syntax error in traditional C as the ellipsis is only allowed following some explicit parameters. (It's not that big a deal; you'd have to #ifdef __cplusplus extern "C" #endif in the header of the C library for C++ compatibility anyway (to prevent function name mangling and thus link errors), so you can #ifdef the () and (...) too.) FWIW, C++ does support the legacy int f(void); syntax, but its use is discouraged except for headers which are also to be used for C code.
- mycroftiv 16y agoI think it's worth mentioning that in the Plan 9 dialect of C, main returning void is absolutely correct. Instead of returning an integer, you call exits() which uses a string argument. The fact that Ken Thompson and Rob Pike made this design choice for Plan 9 is interesting. This is just a related fact, not an attempt to argue that void main is acceptable in standard ansi/iso C.
- drdo 16y agoI don't understand what the discussion is, the standard says the main function should have a return type of int, that should be the end of it, if some random beginner argues "but it works for me", there's not really much you can do, people will continue to write incorrect programs if they want to.
- torpor 16y agoThe big problem with this is not that it teaches people to abuse main() return arguments, but that it covers up a complete lack of understanding of what "void" is doing in this context ..
- zbanks 16y agoJust to be a devil's advocate, void main(void) can be correct when programming microcontrollers & other embedded applications. With GCC, you can even set `void main(void) __attribute__((noreturn));` . (For the lack of a better source, http://www.nongnu.org/avr-libc/user-manual/porting.html http://www.nongnu.org/avr-libc/user-manual/porting.html )