Logo

Programming-Idioms

History of Idiom 205 > diff from v24 to v25

Edit summary for version 25 by nuraby:
New C++ implementation by user [nuraby]

Version 24

2019-10-19, 23:07:21

Version 25

2020-05-22, 10:14:07

Idiom #205 Get an environment variable

Read an environment variable with the name "FOO" and assign it to the string variable foo. If it does not exist or if the system does not support environment variables, assign a value of "none".

Idiom #205 Get an environment variable

Read an environment variable with the name "FOO" and assign it to the string variable foo. If it does not exist or if the system does not support environment variables, assign a value of "none".

Variables
foo
Extra Keywords
envvar os system
Extra Keywords
envvar os system
Imports
#include <cstdlib>
#include <string>
Code
const char* tmp = std::getenv("FOO");
std::string foo = tmp ? std::string(tmp) : "none";
Comments bubble
If environment variable was not found, std::getenv returns nullptr
Doc URL
https://en.cppreference.com/w/cpp/utility/program/getenv