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


Groups > comp.lang.c > #20029

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail
NNTP-Posting-Date Sat, 28 Apr 2012 18:40:23 -0500
Message-ID <4F9C7FE7.1103@mindspring.com> (permalink)
Date Sat, 28 Apr 2012 19:40:23 -0400
From pete <pfiland@mindspring.com>
Reply-To pfiland@mindspring.com
Organization PF
X-Mailer Mozilla 3.04Gold (WinNT; I)
MIME-Version 1.0
Newsgroups comp.lang.c
Subject Re: A short-but-bizarre program that shouldn't work but does.
References <jN-dnRcXh_z89QHSRVn_vwA@giganews.com>
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
Lines 69
X-Usenet-Provider http://www.giganews.com
NNTP-Posting-Host 4.154.217.63
X-Trace sv3-KW1dQx136RYhWL3X7tOUXPkcqsxMtSkRVoNX6XH/ozMVHMeuZsWe7MXtpBuSUKorgXQbn+uwtaSR/XM!3UWLlRpLqQcDTwj7gs0JCNCLjeJQGlcLSP2QvG7DqZqUYGLOuMB4FHCW7gtaPJSKPx8lPXYveFQE!Jz7dRHzN5g==
X-Abuse-and-DMCA-Info Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info Otherwise we will be unable to process your complaint properly
X-Postfilter 1.3.40
X-Original-Bytes 3334
X-Received-Bytes 3475
Xref csiph.com comp.lang.c:20029

Show key headers only | View raw


Robbie Hatley wrote:
> 
> 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.
> The implementation declares no prototype for this function.
> It shall be defined with a return type of int and with no
> parameters:
> int main(void) { /* ... */ }
> or with two parameters (referred to here as argc and argv,
> though any names may be used, as they are local to the function
> in which they are declared):
> int main(int argc, char *argv[]) { /* ... */ }
> 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?

It's called a "Common extension".

ISO/IEC 9899:1999 (E)

Annex J
(informative)
Portability issues
1 This annex collects some information about portability
  that appears in this International Standard.
 
J.5 Common extensions

J.5.1 Environment arguments
1 In a hosted environment, 
  the main function receives a third argument, 
  char *envp[],
  that points to a null-terminated array of pointers to char,
  each of which points to a string 
  that provides information about the environment 
  for this execution of the program (5.1.2.2.1).

-- 
pete

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