Solved

referencing an environment variable in .cc code

Posted on 2004-10-11
5
226 Views
Last Modified: 2010-04-01
How can we reference or set an environment variable inside a .cc file?
Do we define them as macros?
How can it be defined. Please provide an example.
Is there an alternative?

Thanks
0
Comment
Question by:dkamdar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 12278081
You can always obtain the *value* of an environment variable at runtime using 'getenv()', e.g.

/* GETENV.C: This program uses getenv to retrieve
 * the LIB environment variable and then uses
 * _putenv to change it to a new value.
 */

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

void main( void )
{
   char *libvar;

   /* Get the value of the LIB environment variable. */
   libvar = getenv( "LIB" );

   if( libvar != NULL )
      printf( "Original LIB variable is: %s\n", libvar );
}
0
 

Author Comment

by:dkamdar
ID: 12278340
Hey jkr,

Thanks for the reply.
What is that _ before putenv for?
Beacause I saw my code uses putenv().
Can you please explain!

0
 
LVL 86

Expert Comment

by:jkr
ID: 12278356
>>What is that _ before putenv for?

Ask MS :o)

Seriously, they tend to prepend underscores to (most) POSIX-compliant functions for an presumeably unknown reason...
0
 
LVL 3

Expert Comment

by:aravindtj
ID: 12278481
just a small modification for Macro....

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

#define ENVIRON getenv("PATH")
#define ENV_VAL(x) getenv(x)
int main()
{
 printf("Environment Path variable = %s", ENVIRON);
 printf("Environment Prompt Value = %s", ENV_VAL("PROMPT"));
return 0;
}

0
 

Author Comment

by:dkamdar
ID: 12278572
but hey ... I never told I am using MS?
I am on solaris machine.
Anyways i guess it just depends from copiler to compiler as far as naming the system functions go!

Thnaks arvind for that ... but i dont think I would want to compilicate it that way.

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question