Friday, February 13, 2009

[C/C++] Environment variables as argument to main()

Most of us may not have noticed that main() supports a third argument which will provide you an array of pointers to environment variable strings. See the below code,
int main( int nArgc, char* pcArgv[], char* pcenv[ ])
{
int nIdx = 0;
while ( pcenv[ nIdx ] )
printf ( "%s\n", pcenv[ nIdx++ ] );
return 0;
}
The above program will print all the environment variables in the console window

No comments: