getenv() in global space?

Discussion in 'C++' started by silrandir, Feb 23, 2006.

  1. silrandir

    silrandir Guest

    When calling getenv() globally, the function returns NULL, yet when
    called from main, returns an appropriate value.


    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>

    //using namespace std;
    #define MW_DEBUG "TEST"

    char* debugstr = getenv(MW_DEBUG);
    int debug = debugstr?1:0;

    int main()
    {
    if(debug)
    write(2, "\nDEBUG Enabled\n", 15);
    else
    write(2, "\nDEBUG DISABLED\n", 16);
    debugstr = getenv(MW_DEBUG);
    if(debugstr)
    write(2, "\nDebug Enabled\n", 15);
    else
    write(2, "\nDebug Disabled\n", 16);
    }
     
    silrandir, Feb 23, 2006
    #1
    1. Advertisements

  2. So? The behaviour of 'getenv' is implementation-defined. You need to
    read the documentation for your application, perhaps they say that any
    call to 'getenv' is not going to succeed before the 'main' function has
    been called. The platform is probably setting the "environment" just
    before calling your 'main' function.
    V
     
    Victor Bazarov, Feb 23, 2006
    #2
    1. Advertisements

  3. silrandir

    Ian Collins Guest

    No it doesn't, what system are you using?
     
    Ian Collins, Feb 23, 2006
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.