| Program termination | |||||||||||||||||||||
| |||||||||||||||||||||
| Unreachable control flow | |||||||||||||||||||||
(C23) | |||||||||||||||||||||
| Communicating with the environment | |||||||||||||||||||||
| |||||||||||||||||||||
| Memory alignment query | |||||||||||||||||||||
(C23) | |||||||||||||||||||||
| Signals | |||||||||||||||||||||
| Signal types | |||||||||||||||||||||
| Non-local jumps | |||||||||||||||||||||
| Types | |||||||||||||||||||||
Defined in header <stdlib.h> | ||
char*getenv(constchar*name); | (1) | |
errno_t getenv_s(size_t*restrict len,char*restrict value, rsize_t valuesz,constchar*restrict name); | (2) | (since C11) |
getenv, as well as a call to the POSIX functionssetenv(),unsetenv(), andputenv() may invalidate the pointer returned by a previous call or modify the string obtained from a previous call.getenv invokes undefined behavior.getenv_s is only guaranteed to be available if__STDC_LIB_EXT1__ is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__ to the integer constant1 before including<stdlib.h>.Contents |
| name | - | null-terminated character string identifying the name of the environmental variable to look for |
| len | - | pointer to a user-provided location wheregetenv_s will store the length of the environment variable |
| value | - | pointer to a user-provided character array wheregetenv_s will store the contents of the environment variable |
| valuesz | - | maximum number of characters thatgetenv_s is allowed to write todest (size of the buffer) |
On POSIX systems, theenvironment variables are also accessible through the global variableenviron, declared asexternchar**environ; in<unistd.h>, and through the optional third argument,envp, ofthe main function.
The call togetenv_s with a null pointer forvalue and zero forvaluesz is used to determine the size of the buffer required to hold the entire result.
#include <stdio.h>#include <stdlib.h> int main(void){constchar*name="PATH";constchar*env_p= getenv(name);if(env_p)printf("Your %s is %s\n", name, env_p);}
Possible output:
Your PATH is /home/gamer/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/share/games
C++ documentation forgetenv |