?
Solved

referencing an environment variable in .cc code

Posted on 2004-10-11
5
Medium Priority
?
229 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 375 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

777 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