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


Groups > comp.lang.c > #20033

Re: A short-but-bizarre program that shouldn't work but does.

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: A short-but-bizarre program that shouldn't work but does.
Date 2012-04-28 20:18 -0700
Organization None to speak of
Message-ID <ln1un7w7h8.fsf@nuthaus.mib.org> (permalink)
References <jN-dnRcXh_z89QHSRVn_vwA@giganews.com>

Show all headers | View raw


Robbie Hatley <see.my.signature@for.my.contact.info> writes:
> Greetings, group.  I was just looking through my source code
> folders and found the following program, author unknown
> (did I write this myself years ago and forget? dunno).
> According to the C Standard, as I interpret it, this should
> not work... and yet, it does:
>
> #include <stdio.h>
> int main(int argc, char **argv, char **envp)
> {
>     char **envstr = envp;
>     while(*envstr)
>     {
>        puts(*envstr);
>        ++envstr;
>     }
>     return 0;
> }
>
> My expectation: either won't compile, or will crash if ran.
>
> Actuality: gcc compiles this without error.  It does give
> warnings about the unused "argv" and "argc", but it does NOT
> give any error or warning about the illegal third parameter
> "envp".  On execution, the program does not crash, but prints
> a list of all environmental variables and their values.
>
> How can this even compile?
> C99 section 5.1.2.2.1(2) clearly states:
> "The function called at program startup is named main.
[snip]
> or equivalent; or in some other implementation-defined manner."
>
> So, how can main() have *three* parameters if the standard
> demands it have either zero or two?  Why does the above
> program even compile?  Is this some kind of non-standard
> extension or something?

James Kuyper already pointed out the phrase "or in some other
implementation-defined manner".

In addition, that section specifies how the main function "shall be
defined", but when the word "shall" appears outside a constraint, it
states a rule whose violation causes undefined behavior.  There's no
requirement for a diagnostic -- even if the implementation *doesn't*
define
    int main(int argc, char **argv, char **envp) /* ... */
as a valid definition of main.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"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 | Next in thread | Find similar | Unroll thread


Thread

A short-but-bizarre program that shouldn't work but does. Robbie Hatley <see.my.signature@for.my.contact.info> - 2012-04-28 14:59 -0700
  Re: A short-but-bizarre program that shouldn't work but does. Kaz Kylheku <kaz@kylheku.com> - 2012-04-28 22:58 +0000
  Re: A short-but-bizarre program that shouldn't work but does. pete <pfiland@mindspring.com> - 2012-04-28 19:40 -0400
    Re: A short-but-bizarre program that shouldn't work but does. Robbie Hatley <see.my.signature@for.my.contact.info> - 2012-04-28 22:15 -0700
      Re: A short-but-bizarre program that shouldn't work but does. Philip Lantz <prl@canterey.us> - 2012-04-29 01:13 -0700
      Re: A short-but-bizarre program that shouldn't work but does. pete <pfiland@mindspring.com> - 2012-04-29 10:02 -0400
  Re: A short-but-bizarre program that shouldn't work but does. James Kuyper <jameskuyper@verizon.net> - 2012-04-28 22:15 -0400
  Re: A short-but-bizarre program that shouldn't work but does. Keith Thompson <kst-u@mib.org> - 2012-04-28 20:18 -0700
  Re: A short-but-bizarre program that shouldn't work but does. "Heinrich Wolf" <invalid@invalid.invalid> - 2012-04-29 11:20 +0200
    Re: A short-but-bizarre program that shouldn't work but does. "Heinrich Wolf" <invalid@invalid.invalid> - 2012-04-29 11:31 +0200
    Re: A short-but-bizarre program that shouldn't work but does. "Heinrich Wolf" <invalid@invalid.invalid> - 2012-04-29 11:38 +0200
    Re: A short-but-bizarre program that shouldn't work but does. "Heinrich Wolf" <invalid@invalid.invalid> - 2012-04-29 12:26 +0200

csiph-web