C getenv() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C getenv() function reads an environment variable. It searches the host environment for an exact name and returns access to its current value. Environment contents and case rules are platform-dependent.

This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. Success returns a pointer to the variable's value, which may be an empty string. Failure to find the name returns NULL; that differs from a present-but-empty value. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete getenv() call:

C
getenv("PATH");
CallWhat it does
getenv("PATH");Reads an environment variable using the concrete values shown.

Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.

Library declaration
C
char *getenv(const char *name);

Parameters

ParameterDescription
nameNull-terminated environment-variable name.

Return Value

Return ValueDescription
Pointer to stringThe variable exists; the pointed text is owned by the environment.
NULLNo matching variable exists.

C Examples

Example 1 — Read a common variable

This complete program demonstrates a distinct part of the getenv() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>
int main(void){const char *value=getenv("PATH");printf("PATH present: %s\n",value?"yes":"no");}

How It Works

  1. Step 1. Pass the exact variable name.
  2. Step 2. Store the borrowed pointer as read-only text.
  3. Step 3. Check for NULL before using it.
  4. Step 4. Report presence without assuming a particular path value.

Example 2 — Distinguish missing from empty

This complete program demonstrates a distinct part of the getenv() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>
int main(void){const char *v=getenv("CODETOFUN_MISSING_EXAMPLE");if(!v)puts("missing");else if(!*v)puts("empty");else puts("has text");}

How It Works

  1. Step 1. Query a deliberately uncommon name.
  2. Step 2. NULL means the name is absent.
  3. Step 3. A non-NULL pointer may still point to an empty string.
  4. Step 4. Test the first character to separate those states.

Example 3 — Copy a value you must keep

This complete program demonstrates a distinct part of the getenv() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){const char *v=getenv("PATH");if(!v)return 0;size_t n=strlen(v)+1;char *copy=malloc(n);if(!copy)return EXIT_FAILURE;memcpy(copy,v,n);puts("copied");free(copy);}

How It Works

  1. Step 1. Obtain the environment-owned text.
  2. Step 2. Measure enough space including the null terminator.
  3. Step 3. Copy into checked caller-owned storage.
  4. Step 4. Free the independent copy after use.

Frequently Asked Questions

getenv() reads an environment variable. It searches the host environment for an exact name and returns access to its current value. Environment contents and case rules are platform-dependent.
Include <stdlib.h> before calling getenv().
Success returns a pointer to the variable's value, which may be an empty string. Failure to find the name returns NULL; that differs from a present-but-empty value.
Do not free or modify the returned string. A later environment-changing operation may invalidate it; copy the text if it must be retained.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.

Next: labs()

Continue through the C stdlib.h tutorials.

labs() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful