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> Date: Sat, 28 Apr 2012 19:40:23 -0400 From: pete 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: 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 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 > 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