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.

 S.no Function  Description 
  1  getenv()

This function gets the current value of the environment variable

  2  setenv()

This function sets the value for environment variable

  3  putenv()

This function modifies the value for environment variable

  4  perror()

Displays most recent error that happened during library function call

  5  rand()

Returns random integer number range from 0 to at least 32767

  6  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 = %sn", 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.
#include <stdio.h>
#include <stdlib.h>
int main()
{
   setenv("FILE","/usr/bin/example.c",50);
   printf("File = %sn", 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.
#include <stdio.h>
#include <stdlib.h>
int main()
{
   setenv("DIR","/usr/bin/example/",50);
   printf("Directory name before modifying = " 
          "%sn", getenv("DIR"));

   putenv("DIR=/usr/home/");
      printf("Directory name after modifying = " 
             "%sn", 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.

#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

#include<stdio.h>
#include<stdlib.h>

#include<time.h>

int main ()

{

   printf ("1st random number :  %dn", rand() % 100);
   printf ("2nd random number : %dn", rand() % 100);
   printf ("3rd  random number: %dn", 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.

#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

      Prev                                                                                              Next

.