

Void main(void) - the Wrong Thing - gnosis
http://users.aber.ac.uk/auj/voidmain.shtml

======
m0nastic
I 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
Your 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
I 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).

------
guylhem
For 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
seriously? i knew that shit when i was 15. taught myself C on an amiga 500.

------
beej71
Besides, 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
> And, horridly, return isn't necessary in main,

Only true for C++.

~~~
hoop

        but it works for me!

~~~
henning
Works On My Machine(tm) [http://www.codinghorror.com/blog/2007/03/the-works-
on-my-mac...](http://www.codinghorror.com/blog/2007/03/the-works-on-my-
machine-certification-program.html)

------
mycroftiv
I 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.

------
gnosis
Also see:

<http://jk-technology.com/ctips01.html#int_main>

and

<http://www.eskimo.com/~scs/readings/voidmain.960823.html>

------
zbanks
Just 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> )

------
brownleej
I 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?

~~~
pmjordan
You'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.

------
drdo
I 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.

