Table of Contents
Time.H Header File In C With Examples
The time.h
header file in C contains functions for obtaining and manipulating date and time information. It introduces three time-related data types:
clock_t
:
- Represents the processor time used by the program as an integer.
- It is a part of the calendar time, measuring the CPU time taken for the execution of a program.
2.time_t
:
- Represents the calendar time as an integer.
- It is a part of the calendar time and is commonly used to store time values, often representing the number of seconds elapsed since a specific epoch.
3.struct tm
:
- Holds the date and time information in a structured format.
- Contains members such as seconds, minutes, hours, day of the month, month, year, and more.
- Provides a convenient way to access and manipulate individual components of a date and time.
In summary, the time.h
header file facilitates time-related operations in C programming, offering data types like clock_t
and time_t
for specific time representations, and struct tm
for a structured approach to handling date and time information.
struct tm { // seconds, range 0 to 59 int tm_sec; // minutes, range 0 to 59 int tm_min; // hours, range 0 to 23 int tm_hour; // day of the month, range 1 to 31 int tm_mday; // month, range 0 to 11 int tm_mon; // The number of years since 1900 int tm_year; // day of the week, range 0 to 6 int tm_wday; // day in the year, range 0 to 365 int tm_yday; // daylight saving time int tm_isdst;}
This will have a CLOCKS_PER_SEC macro that will contain the number of times the system clock ticks per second.
Pre-defined functions in time.h
S.No | Function Name | Explanation |
---|---|---|
1. | asctime() | This function will 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 will returns the processor time consumed by a program |
3. | ctime() | ctime() will return 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 Time |
4. | difftime() | This function returns the difference between the time. |
5. | gmtime() | This function will print the UTC (Coordinated Universal Time) Time and date. The format for both gmtime() and asctime() is the same. |
6. | mktime() | This function will print the UTC (Coordinated Universal Time) Time and date. Format for both gmtime() and asctime() is the same. |
7. | time() | This mktime() will returns the calendar-time equivalent using struct tm. |
8. | strftime() | This strftime() will format the string returned by other time functions using different format specifiers |
Example
1.Program to print the date and time of the system
#include <stdio.h>#include <time.h>int main(void){ struct tm* ptr; time_t t; t = time(NULL); ptr = localtime(&t); printf("%s", asctime(ptr)); return 0;}
Output
Tue Aug 6 09:00:29 2019
2. Program to print UTC (Coordinated Universal Time) of the system
#include <stdio.h>#include <time.h>int main(void){ struct tm* ptr; time_t t; t = time(NULL); ptr = gmtime(&t); printf("%s", asctime(ptr)); return 0;}
Output
Tue Aug 6 09:00:31 2019
3. 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>int main(void){ time_t start, end; start = time(NULL); int a, 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));}
Output
Sum of 4196144 and 0 is 4196144Time taken to print sum is 0.00 seconds
4. Program to find the clock ticks
#include <math.h>#include <stdio.h>#include <time.h> int frequency_of_primes(int n){ // This function checks the number of // primes less than the given parameter int i, j; int freq = n - 1; for (i = 2; i <= n; ++i) for (j = sqrt(i); j > 1; --j) if (i % j == 0) { --freq; break; } return freq;} int main(){ clock_t t; int f; 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); return 0;}
Output
The number of primes lower than 10, 000 is: 1229No. of clicks 2837 clicks (0.002837 seconds).
5. Program to print time as hour: minute returned by asctime() file
#include <stdio.h>#include <time.h>int main(){ time_t rawtime; struct tm* timeinfo; // Used to store the time // returned by localetime() function char buffer[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 time puts(buffer); return 0;}
Output
Time is 09:00AM.
FAQ- Time.H Header File In C With Examples
Q1. What is the header for time in C?
Ans. The time.h
header file in C and C++ helps with handling time and date operations. It provides functions and structures to work with time-related tasks.
Q2. What is time_t structure in C?
Ans. The time.h
header file in C and C++ helps with handling time and date operations. It provides functions and structures to work with time-related tasks.
Q3. What is header file in C with example?
Ans. A C header file is indeed a text file containing C code, typically with a .h file extension. Inclusion in a program is done using the #include
preprocessor directive. This allows you to include the contents of the header file in your C program, making functions, declarations, and other code available for use
Hridhya Manoj
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together