Monday, May 16, 2011

getenv in C

getenv in C

this is to get the environmental variable's value in Shell from C file

to store it to a variable use:

fp=getenv("PATH")

The following program will explain it more clearly


This program demonstrates how to get a value for environmental value in shell
and compare it with a predefined variable.

1 #include
2 #include
3 #include
4 int main()
5 {
6 char * cc;
7 cc = getenv("HOME");
8 printf("--->%s<--",cc);
9 if ( strcmp(cc,"/home/karthik") == 0 )
10 {
11 printf("yes the home is /home/karthik");
12 }
13 return 0;
15 }

No comments:

Post a Comment