Menu

#1970 environ[] variable is undeclared if to compile in C99 compatibility mode

WSL
closed
None
Support
invalid
User_Error
False
2014-08-12
2013-05-12
No

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;
};

Discussion

  • Earnie Boyd

    Earnie Boyd - 2013-05-12
    • status: unread --> assigned
    • assigned_to: Earnie Boyd
    • Group: OTHER --> WSL
    • Type: Bug --> Support
    • Resolution: none --> invalid
    • Category: Unknown --> User_Error
     
  • Earnie Boyd

    Earnie Boyd - 2013-05-12

    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.

    gcc -std=c99 -U__STRICT_ANSI__ 1.c
    

    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
  • Earnie Boyd

    Earnie Boyd - 2013-05-12

    Another option is to use the main() function to get the list of environment variables.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[], char **envp)
    {
        int i;
        char *s;
    
        while (s=envp[i])
        {
            printf ("%s\n", s);
            i++;
        };
    
        return 0;
    };
    
    gcc -std=c99 1.c
    
     
  • dennis.yurichev

    dennis.yurichev - 2013-05-12

    Hmm, environ isn't supported in MinGW's C99 implementation in other words?

     
  • Keith Marshall

    Keith Marshall - 2013-05-12

    Hmm, environ isn't supported in MinGW's C99 implementation in other words?

    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.

    environ global variable is supported in POSIX... http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html

    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

     
  • dennis.yurichev

    dennis.yurichev - 2013-05-12

    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?

     
    • Keith Marshall

      Keith Marshall - 2013-05-12

      I thought I'd already stated this perfectly clearly:

      ... is environ global variable (defined in POSIX)

      Yes, it is defined by, and required by POSIX.

      is defined in ANSI C (C89 and C90)?

      No, it is not defined by, nor permitted when you request conformity checking with, either variant of ANSI C (a.k.a. ISO C).

      If not, should it be accessed without -std=c99 switch then?

      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).

       
  • Earnie Boyd

    Earnie Boyd - 2013-05-12
    • status: assigned --> closed
     
  • Earnie Boyd

    Earnie Boyd - 2013-05-12

    I think we have an understanding of facts. There is no bug to resolve other than the one introduced by the user code itself.