Prev Next
Descriptions and example programs for C environment functions such as getenv(), setenv(), putenv() and other functions perror(), random() and delay() are given below.
Miscellaneous functions | Description |
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.
1 2 3 4 5 6 7 8 |
#include <stdio.h> #include <stdlib.h> int main() { printf("Directory = %s\n", getenv("DIR")); return 0; } |
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.
1 2 3 4 5 6 7 8 |
#include <stdio.h> #include <stdlib.h> int main() { setenv("FILE","/usr/bin/example.c",50); printf("File = %s\n", getenv("FILE")); return 0; } |
Output:
File = /usr/bin/example.c |
Example program for putenv() function in C:
- This function modifies the value of environment variable.
- Below example program shows that how to modify an existing environment variable value.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#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; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#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; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#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; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#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; } |
Output:
Suspends the execution of the program for particular time |