On a Unix-like system, you can use the setenv(3) library function. I believe
this is also a documented part of the C90 standard, but I haven't checked it
to confirm.
No, it isn't. If you do man setenv you will probably see something like
"conforming to BSD 4.3" and possibly POSIX.1, not to the C standards.
However, it doesn't do what you want anyway, it modifies the current
environment not the parent environment so when the program exits that
environment is lost and set up from the environment of the parent
(shell) when the program is executed again.
| int
| setenv(const char *name, const char *value, int overwrite);
It's worth noting that an environment variable will persist across multiple
executions, provided they happen in the same environment.
No, it won't unless it is done in the parent environment. There is
nothing in Unix/POSIX which allows you to change the parent environment
(or in MSDOS, at least not portably from one version to another).
Followups set to comp.unix.programmer where it is on topic.
In other words, if
you log out, your environment is gone. If that's an issue, it's probably a
better idea to write a file instead.
That is the only portable way to do persistent variables in C (or most
other languages, although some provide library facilities to make it
easy).
Chris C