Printing envrioment variables in C

Associate
Joined
28 Feb 2009
Posts
519
Does anybody know how to print the user name in C. Im writing a Shell in Linux and need to be able to print the user name within the prompt.
Many Thanks
 
Not gonna give you too much help with your coursework, but I will say that there is (afaik) no function for *Printing* an environment variable per se, but there is a standard library function that can *Get* an environment variable, which you can then use printf to output to the console...
 
Does anybody know how to print the user name in C. Im writing a Shell in Linux and need to be able to print the user name within the prompt.
Many Thanks

Took me a whole 25 seconds to find this using Google :o

#include <stdlib.h>
int main(int argc, char *argv[])
{
char *p=getenv("USER");
if(p==NULL) return EXIT_FAILURE;
printf("%s\n",p);
return 0;

}
 
Back
Top Bottom