This blog is under construction

Tuesday 9 July 2013

C program to print date and sleep for a given time

Write a C program to print date and sleep for a given time.


  #include <stdio.h>
  #include <time.h>

  int main() {
        int seconds;
        time_t ts;
        struct tm *ct;

        /* get the input in seconds */
        printf("Enter input in seconds to sleep:");
        scanf("%d", &seconds);

        /* sleep for given time */
        printf("Going to sleep for %d seconds\n", seconds);
        sleep(seconds);
        printf("Awaken!!\n");

        /* time in seconds */
        ts = time(NULL);

        /* converting above time value to local time */
        ct = localtime(&ts);

        /* print the results */
        printf("Year: %d\n", 1900 + ct->tm_year);
        printf("Month: %d\n", ct->tm_mon + 1);
        printf("Day: %d\n", ct->tm_mday);
        printf("Time: %02d:%02d:%02d\n",
                ct->tm_hour, ct->tm_min, ct->tm_sec);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out 
  Enter input in seconds to sleep:10
  Going to sleep for 10 seconds
  Awaken!!
  Year: 113
  Month: 6
  Day: 8
  Time: 23:59:41





See Also:

No comments:

Post a Comment