Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #2988

Re: Just starting out in C

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Just starting out in C
Date 2011-04-28 11:51 -0700
Organization None to speak of
Message-ID <ln1v0mxl8y.fsf@nuthaus.mib.org> (permalink)
References <1c7cbaf6-ca06-4cd0-a287-48b46e0f2540@r19g2000prm.googlegroups.com> <ln4o5m293q.fsf@nuthaus.mib.org> <884f54d7-7679-4cba-b6b4-aa95d6245752@n10g2000yqf.googlegroups.com>

Show all headers | View raw


John Bode <jfbode1029@gmail.com> writes:
> On Apr 25, 12:28 am, Keith Thompson <ks...@mib.org> wrote:
[...]
>> "void main()" is wrong, and I'd like to know where the &*^&
>> the authors got the idea that it's correct; K&R2 certainly
>> never suggests it.  The correct declaration is "int main(void)";
>> "int main()" is probably acceptable.  Many compilers will accept
>> "void main()" without complaint, but it's particularly important
>> in C to understand that just because something appears to work,
>> that doesn't mean it's correct.
>>
>
> To expand on this a bit, there is a standards document that defines
> the C programming language (an online version can be found at
> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf).  There's a
> section that defines the allowable signatures for main in a hosted
> implementation (such as gcc running under Cygwin), and the choices
> are:
>
>     int main(int argc, char **argv) /* or char *argv[], the syntax is
> equivalent */
>
> or
>
>     int main(void) /* the "void" indicates that main takes no
> arguments */
>
> or *in some other implementation-defined manner*, meaning that the
> implementation has to document it as a legal signature.  Unless the
> gcc documentation *explicitly* says that "void main()" is kosher (and
> I'm pretty sure it doesn't), you shouldn't use it.

There appears to be no occurrence of "void main" in the gcc
documentation.

But even if a compiler does document "void main()" (or "void
main(void)") as a valid declaration for main, there's still no good
reason to use it, and some very good reasons not to.  In such an
implementation, it's likely that this:
    void main(void) { }
is equivalent to this:
    int main(void) { return 0; }
(the return statement can be omitted in C99).  So just write it
that way!  Using the implementation-defined form gains you exactly
nothing, and costs you portability to implementations that don't
support it.

Note that even if your program appears to work, it might fail in
some subtle way.  And even if it always works (on an implementation
that supports void main() but doesn't document it), you'll always
have to wonder, every time your program malfunctions, whether the
non-portable declaration of main might be the culprit.

Note that all of this applies *only* to hosted implementations.
Freestanding implementations (basically implementations where
the program doesn't necessarily run under an operating system)
can place their own requirements on your program's entry point.
On some such systems, "void main(void)" might be the *only* valid
way to declare main -- or the entry point might have a different
name altogether.  But if you're just learning C, you're probably
not using a freestanding implementation.

> To be safe, every time you see "void main()" in the tutorial (at least
> until they get up to command line arguments), replace it with "int
> main(void)".

To be even safer, every time you see "void main()" in a tutorial,
find yourself a different tutorial.  It's not necessarily a fatal
error by itself, but it tends to be indicative of a general ignorance
about C, particularly about the distinction between C the language
and particular implementations of it.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Just starting out in C Robert Miles <robertmilesxyz@gmail.com> - 2011-04-24 17:00 -0700
  Re: Just starting out in C Geoff <geoff@invalid.invalid> - 2011-04-24 17:16 -0700
    Re: Just starting out in C Keith Thompson <kst-u@mib.org> - 2011-04-24 22:06 -0700
  Re: Just starting out in C Mickey Mouse <fake@email.com> - 2011-04-24 19:54 -0700
  Re: Just starting out in C Keith Thompson <kst-u@mib.org> - 2011-04-24 22:08 -0700
  Re: Just starting out in C Keith Thompson <kst-u@mib.org> - 2011-04-24 22:28 -0700
    Re: Just starting out in C BruceS <bruces42@hotmail.com> - 2011-04-25 06:34 -0700
    Re: Just starting out in C pete <pfiland@mindspring.com> - 2011-04-25 12:29 -0400
    Re: Just starting out in C Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-04-27 14:48 +0000
    Re: Just starting out in C John Bode <jfbode1029@gmail.com> - 2011-04-28 09:55 -0700
      Re: Just starting out in C Keith Thompson <kst-u@mib.org> - 2011-04-28 11:51 -0700

csiph-web