C – Miscellaneous functions
Descriptions and example programs for C environment functions such as getenv(), setenv(), putenv() and
other functions perror(), random() and delay() are given below.
Miscellaneous functionsDescription
getenv() This function gets the current value of the environment variable
setenv() This function sets the value for environment variable
putenv() This function modifies the value for environment variable
perror()Displays most recent error that happened during library function call
rand() Returns random integer number range from 0 to at least 32767
delay() Suspends the execution of the program for particular time
EXAMPLE PROGRAM FOR GETENV() FUNCTION IN C:
This function gets the current value of the environment variable.
Let us assume that environment variable DIR is assigned to “/usr/bin/test/”. Below program will show
you how to get this value using getenv() function.
#include <stdio.h>
#include <stdlib.h>
int main()
printf("Directory = %s\n", getenv("DIR"));
return 0;
}
COMPILE & RUN
OUTPUT:
/usr/bin/test/
EXAMPLE PROGRAM FOR SETENV() FUNCTION IN C:
This function sets the value for environment variable.
Let us assume that environment variable “FILE” is to be assigned “/usr/bin/example.c”. Below program
will show you how to set this value using setenv() function.
#include <stdio.h>
#include <stdlib.h>
int main()
setenv("FILE","/usr/bin/example.c",50);
printf("File = %s\n", getenv("FILE"));
return 0;
COMPILE & RUN
OUTPUT:
File = /usr/bin/example.c
EXAMPLE PROGRAM FOR PUTENV() FUNCTION IN C:
This function modifies the value of environment variable.
#include <stdio.h>
#include <stdlib.h>
int main()
setenv("DIR","/usr/bin/example/",50);
printf("Directory name before modifying = " \
"%s\n", getenv("DIR"));
putenv("DIR=/usr/home/");
printf("Directory name after modifying = " \
"%s\n", getenv("DIR"));
return 0;
COMPILE & RUN
OUTPUT:
Directory name before modifying = /usr/bin/example/
Directory name after modifying = /usr/home/
EXAMPLE PROGRAM FOR PERROR() FUNCTION IN C:
This function displays most recent error that happened during library function call.
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
int main()
FILE *fp;
char filename[40] = "test.txt";
/* Let us consider test.txt not available */
fp = f open(filename, "r");
if(fp == NULL)
perror("File not found");
printf("errno : %d.\n", errno);
return 1;
printf("File is found and opened for reading");
fclose(fp);
return 0;
COMPILE & RUN
OUTPUT:
errno : 22.
File not found: No such file or directory
EXAMPLE PROGRAM FOR RAND() FUNCTION IN C:
This function returns the random integer numbers range from 0 upto 32767.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main ()
printf ("1st random number : %d\n", rand() % 100);
printf ("2nd random number : %d\n", rand() % 100);
printf ("3rd random number: %d\n", rand());
return 0;
COMPILE & RUN
OUTPUT:
1st random number : 83
2nd random number : 86
3rd random number: 16816927
EXAMPLE PROGRAM FOR DELAY() FUNCTION IN C:
This function suspends the execution of the program for particular time.
#include<stdio.h>
#include<stdlib.h>
int main ()
printf("Suspends the execution of the program " \
"for particular time");
delay(5000); // 5000 mille seconds
return 0;
COMPILE & RUN
OUTPUT:
Suspends the execution of the program for particular time