putenv() sets the program environment variables. How do you set the OS environment variables? I want to be able to set an environment variable that will stay after the program ends.
Thanks
putenv() sets the program environment variables. How do you set the OS environment variables? I want to be able to set an environment variable that will stay after the program ends.
Thanks
What OS/compiler ?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
16-bit DOS. I'm using the MARS 16 bit compiler.
Your help is appreciated.
Thanks
You could use 'system' with a SET command.
Code:#include <stdio.h> #include <stdlib.h> int main () { int i; puts ("Trying to set environment variables"); i = system ("SET BLASTER 220 5 1"); if (i==-1) puts ("Error setting variables"); else puts ("Environment variables set"); return 0; }
nb: Code blatently stolen and changed to fit your question.
Demonographic rhinology is not the only possible outcome, but why take the chance
> i = system ("SET BLASTER 220 5 1");
This creates a sub-process which gets an environment variable set for its very short existance and then all is lost again when the sub-process exits.
It is possible to 'hack' the parent environment in real DOS (not any win32 console, nor any POSIX operating system), but as I recall, that involved some rather tricky assembler work.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.