time.h header file in C with Examples
The time.h header file contains definitions of functions to get and manipulate date and time information.
- It describes three time related datatypes.
- clock_t: clock_t represents the date as integer which is a part of the calendar time.
- time_t: time_t represents the clock time as integer which is a part of the calendar time.
- struct tm: struct tm holds the date and time which contains:
structtm{// seconds, range 0 to 59inttm_sec;// minutes, range 0 to 59inttm_min;// hours, range 0 to 23inttm_hour;// day of the month, range 1 to 31inttm_mday;// month, range 0 to 11inttm_mon;// The number of years since 1900inttm_year;// day of the week, range 0 to 6inttm_wday;// day in the year, range 0 to 365inttm_yday;// daylight saving timeinttm_isdst;}chevron_rightfilter_none
- It also contains CLOCKS_PER_SEC macro which holds the number of times does the system clock ticks per second.
- Pre-defined functions in time.h
S.No Function Name Explanation 1. asctime() This function returns the date and time in the format
day month date hours:minutes:seconds year.
Eg: Sat Jul 27 11:26:03 2019.
asctime() function returns a string by taking struct tm variable as a parameter.2. clock() This function returns the processor time consumed by a program 3. ctime() This function returns the date and time in the format
day month hours:minutes:seconds year
Eg: Sat Jul 27 11:26:03 2019
time is printed based on the pointer returned by Calendar Time4. difftime() This function returns the difference between the times provided. 5. gmtime() This function prints the UTC (Coordinated Universal Time) Time and date.
Format for both gmtime() and asctime() is same6. mktime() This function returns the calendar-time equivalent using struct tm. 7. time() This function returns the calendar-time equivalent using data-type time_t. 8. strftime() This function helps to format the string returned by other time functions using different format specifiers - Examples:
- Program to print the date and time of the system.
#include <stdio.h>#include <time.h>intmain(void){structtm* ptr;time_tlt;lt =time(NULL);ptr =localtime(<);printf("%s",asctime(ptr));return0;}chevron_rightfilter_noneOutput:Tue Aug 6 09:00:29 2019
- Program to print UTC (Coordinated Universal Time) of the system.
#include <stdio.h>#include <time.h>intmain(void){structtm* ptr;time_tlt;lt =time(NULL);ptr =gmtime(<);printf("%s",asctime(ptr));return0;}chevron_rightfilter_noneOutput:
Tue Aug 6 09:00:31 2019
- Program to calculate the time taken to add two numbers program.
Note: If user gives input slowly that time also add up for total execution time.#include <stdio.h>#include <time.h>intmain(void){time_tstart, end;start =time(NULL);inta, b;scanf("%d %d", &a;, &b;);printf("Sum of %d and %d is %d\n",a, b, a + b);end =time(NULL);printf("Time taken to print sum is %.2f seconds",difftime(end, start));}chevron_rightfilter_noneOutput:Sum of 4196144 and 0 is 4196144 Time taken to print sum is 0.00 seconds
- Program to find the clock ticks.
#include <math.h>#include <stdio.h>#include <time.h>intfrequency_of_primes(intn){// This function checks the number of// primes less than the given parameterinti, j;intfreq = n - 1;for(i = 2; i <= n; ++i)for(j =sqrt(i); j > 1; --j)if(i % j == 0) {--freq;break;}returnfreq;}intmain(){clock_tt;intf;t =clock();f = frequency_of_primes(9999);printf("The number of primes lower"" than 10, 000 is: %d\n",f);t =clock() - t;printf("No. of clicks %ld clicks (%f seconds).\n",t, ((float)t) / CLOCKS_PER_SEC);return0;}chevron_rightfilter_noneOutput:The number of primes lower than 10, 000 is: 1229 No. of clicks 2837 clicks (0.002837 seconds).
- Program to print time as hour: minute returned by asctime() file.
#include <stdio.h>#include <time.h>intmain(){time_trawtime;structtm* timeinfo;// Used to store the time// returned by localetime() functioncharbuffer[80];time(&rawtime;);timeinfo =localtime(&rawtime;);strftime(buffer, 80,"Time is %I:%M%p.",timeinfo);// strftime() function stores the// current time as Hours : Minutes//%I %M and %p-> format specifier// of Hours minutes and am/pm respectively*/// prints the formatted timeputs(buffer);return0;}chevron_rightfilter_noneOutput:Time is 09:00AM.
- Program to print the date and time of the system.
Recommended Posts:
- How to write your own header file in C?
- Comment in header file name?
- Difference between Header file and Library
- Print "Hello World" in C/C++ without using any header file
- C program to copy contents of one file to another file
- numeric header in C++ STL | Set 1 (accumulate() and partial_sum())
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Namespace in C++ | Set 3 (Accessing, creating header, nesting and aliasing)
- C program for file Transfer using UDP
- fsetpos() (Set File Position) in C
- Basics of File Handling in C
- What is data type of FILE in C ?
- C program to delete a file
- C | File Handling | Question 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : avsadityavardhan



