Home »
C solved programs »
C miscellaneous programs
C program to print all environment variables
Here, we are going to learn how to print all environment variables using C program?
By Nidhi Last updated : March 10, 2024
Problem Solution
Here, we will print all environment variables using the 3rd parameter in the main() function.
Program
The source code to print all environment variables is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
// C program to print all environment variables
#include <stdio.h>
#include <string.h>
void main(int argc, char* argv[], char* envVar[])
{
int i = 0;
while (envVar[i] != NULL) {
printf("%s\n", envVar[i]);
i++;
}
}
Output
$ gcc main.c -o main
$./main
VTE_VERSION=6003
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/7aada8bd_c73c_4b7d_bb5c_83221714dd99
INVOCATION_ID=8c7fb8541f904c58a01d545e79373e93
MANAGERPID=1441
GJS_DEBUG_OUTPUT=stderr
LESSCLOSE=/usr/bin/lesspipe %s %s
XDG_SESSION_CLASS=user
TERM=xterm-256color
LESSOPEN=| /usr/bin/lesspipe %s
USER=arvind
GNOME_TERMINAL_SERVICE=:1.120
DISPLAY=:0
SHLVL=1
QT_IM_MODULE=ibus
XDG_RUNTIME_DIR=/run/user/1000
JOURNAL_STREAM=8:52131
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
GDMSESSION=ubuntu
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
OLDPWD=/home/arvind/Desktop
Explanation
Here, we created a function main(), and in the main() function we printed all environment variables using the 3rd parameter.
C Miscellaneous Programs »