The code below is compiling fine in latest MinGW (GCC 4.7.2) but failing when -std=c99 switch is applied:
$ cc -std=c99 1.c
1.c: In function 'main':
1.c:9:14: error: 'environ' undeclared (first use in this function)
#include <stdio.h> #include <stdlib.h> int main() { int i; char *s; while (s=environ[i]) { printf ("%s\n", s); i++; }; return 0; };
The question is should environ be declared with __STRICT_ANSI__ defined?
Using -std=c99 defines __STRICT_ANSI__, if you don't want __STRICT_ANSI__ you'll need to do the following.
I've looked but cannot find a reference to the environ variable being ANSI supported. If you can point me to a reference I can make modifications otherwise this issue will be closed.
Last edit: Earnie Boyd 2013-05-12
Another option is to use the main() function to get the list of environment variables.
Hmm, environ isn't supported in MinGW's C99 implementation in other words?
environ global variable is supported in POSIX... http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
By specifying -std=c99, you've asked the compiler to check that your code is strictly ISO-C99 conforming; environ is not defined in C99, so your strictly conforming application is not allowed to use it.
This is irrelevant; POSIX is not the same as ISO-C99. If all you want is POSIX conformity checking, use -posix instead of -std=c99.
I don't see any bug here; just a misunderstanding on your part, of the purpose of standards conformity checking options, such as -std=c99
OK, but may I ask, is environ global variable (defined in POSIX) is defined in ANSI C (C89 and C90)?
If not, should it be accessed without -std=c99 switch then?
I thought I'd already stated this perfectly clearly:
Yes, it is defined by, and required by POSIX.
No, it is not defined by, nor permitted when you request conformity checking with, either variant of ANSI C (a.k.a. ISO C).
You already know the answer to this, since your original report states that you can use environ when you don't specify -std=c99. When you specify that switch, you tell the compiler that it must not allow you to use any feature which is not defined by the standard; environ is one such feature, which the compiler is correctly telling you that you may not use, when you request this level of standards conformity. When you don't request this level of standards enforcement, then of course you may use environ, (among many other extensions to the ISO C standards, which are defined by POSIX, or as GNU extensions).
I think we have an understanding of facts. There is no bug to resolve other than the one introduced by the user code itself.