Show Menu
Cheatography

C++ (C standard library functions) Cheat Sheet (DRAFT) by

A cheat sheet listing command C standard library functions C++ can call.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

setenv()

setenv("MY_VARIABLE", "my_value", 1);  // set and overwrite if exists
setenv("MY_VARIABLE", "my_value", 0);  // set only if not already set

// commonly used to modify PATH
setenv("PATH", new_path.c_str(), 1);
Sets or updates an enviro­nment variable for the current process and any child processes it spawns.

Takes three arguments: the variable name, the value, and an overwrite flag.

1 means overwrite if the variable already exists, 0 means leave it unchanged if it exists.
 

unsetenv()

unsetenv("MY_VARIABLE");   // removes MY_VARIABLE from the environment

// verify it was removed
const char* value = std::getenv("MY_VARIABLE");
// value == nullptr
Removes an enviro­nment variable from the current running program and any child processes it spawns.

After calling it the variable no longer exists, so std::g­etenv() would return nullptr for it.

Other programs running on the system are completely unaffected — each process has its own copy of enviro­nment variables.