getenv() Function

The getenv() function is declared in the header file <stdlib.h>.

The getenv() function is used to retrieve the value of an environment variable. It is an easy way to access configuration settings and system properties stored in the environment without having to parse them manually.


Syntax of getenv()

</>
Copy
char *getenv(const char *name);

Parameters

ParameterDescription
nameA C-string containing the name of the environment variable to be retrieved. Note that depending on the platform, the name may be case sensitive.

It is worth mentioning that the pointer returned by getenv() refers to an internal memory block. This memory may be modified by subsequent calls to getenv() or by specific environment-modifying functions. Therefore, the returned string should not be altered by the program.


Return Value

The function returns a pointer to a C-string containing the value of the requested environment variable. If the environment variable does not exist, it returns a null pointer.


Examples for getenv()

Example 1: Retrieving an Existing Environment Variable

This example demonstrates how to retrieve the value of a common environment variable and display it.

Program

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    char *path = getenv("PATH");

    if (path != NULL) {
        printf("The PATH environment variable is:\n%s\n", path);
    } else {
        printf("PATH environment variable not found.\n");
    }

    return 0;
}

Explanation:

  1. The program calls getenv() to retrieve the value of the environment variable named "PATH".
  2. If the variable exists, its value is printed.
  3. If not, a message indicating that the variable was not found is displayed.

Program Output:

The PATH environment variable is:
/opt/swift/swift-5.7.3-RELEASE-ubuntu22.04/usr/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Example 2: Handling a Non-Existent Environment Variable

This example shows how to safely handle the case when an environment variable does not exist.

Program

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    char *value = getenv("NON_EXISTENT_VAR");

    if (value == NULL) {
        printf("Environment variable NON_EXISTENT_VAR does not exist.\n");
    } else {
        printf("Value: %s\n", value);
    }

    return 0;
}

Explanation:

  1. The program attempts to retrieve the value of an environment variable named "NON_EXISTENT_VAR".
  2. Since this variable is not set, getenv() returns a null pointer.
  3. The program checks for a null pointer and prints an appropriate message.

Program Output:

Environment variable NON_EXISTENT_VAR does not exist.

Example 3: Using getenv() to Retrieve Custom Environment Variables

This example demonstrates how a program can use getenv() to access a custom environment variable that might be set by the user or the system.

Program

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    char *custom = getenv("MY_CUSTOM_VAR");

    if (custom != NULL) {
        printf("MY_CUSTOM_VAR is set to: %s\n", custom);
    } else {
        printf("MY_CUSTOM_VAR is not set.\n");
    }

    return 0;
}

Explanation:

  1. The program checks for the environment variable "MY_CUSTOM_VAR" using getenv().
  2. If the variable is set, its value is printed.
  3. If the variable is not set, the program notifies the user that it is missing.

Program Output:

MY_CUSTOM_VAR is not set.

Example 4: Chaining getenv() Calls for Multiple Variables

This example illustrates how to retrieve and display multiple environment variables in one program.

Program

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    char *path = getenv("PATH");
    char *home = getenv("HOME");

    if (path != NULL) {
        printf("PATH: %s\n", path);
    } else {
        printf("PATH variable not found.\n");
    }

    if (home != NULL) {
        printf("HOME: %s\n", home);
    } else {
        printf("HOME variable not found.\n");
    }

    return 0;
}

Explanation:

  1. The program retrieves two environment variables: "PATH" and "HOME".
  2. It checks whether each variable exists.
  3. If a variable exists, its value is printed; otherwise, a not-found message is displayed.

Program Output:

PATH: /opt/swift/swift-5.7.3-RELEASE-ubuntu22.04/usr/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOME: /home/tutorialkart