x

C – Miscellaneous functions

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.

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.

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.

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.


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.

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.

Output:

Suspends the execution of the program for particular time

Prev     Next



Like it? Please Spread the word!