{"id":3679,"date":"2024-03-06T11:41:23","date_gmt":"2024-03-06T11:41:23","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3679"},"modified":"2024-03-06T11:41:23","modified_gmt":"2024-03-06T11:41:23","slug":"time-h-header-file-in-c-with-examples","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/time-h-header-file-in-c-with-examples\/","title":{"rendered":"Time.H Header File In C With Examples"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#time-h-header-file-in-c-with-examples\">Time.H Header File In C With Examples<\/a><\/li><li ><a href=\"#pre-defined-functions-in-time-h\">Pre-defined functions in time.h<\/a><\/li><li ><a href=\"#example\">Example<\/a><ul><li ><a href=\"#1-program-to-print-the-date-and-time-of-the-system\">1.Program to print the date and time of the system<\/a><\/li><li ><a href=\"#2-program-to-print-utc-coordinated-universal-time-of-the-system\">\u00a0 2. Program to print UTC (Coordinated Universal Time) of the system<\/a><\/li><li ><a href=\"#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\">\u00a0\u00a0 \u00a03. Program to calculate the time taken to add two numbers program.\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Note:\u00a0If user gives input slowly that time also add up for total execution time.\u00a0<\/a><\/li><li ><a href=\"#4-program-to-find-the-clock-ticks\">4. Program to find the clock ticks<\/a><\/li><li ><a href=\"#5-program-to-print-time-as-hour-minute-returned-by-asctime-file\">5. Program to print time as hour: minute returned by asctime() file<\/a><\/li><\/ul><\/li><li ><a href=\"#faq-time-h-header-file-in-c-with-examples\">FAQ- Time.H Header File In C With Examples<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"time-h-header-file-in-c-with-examples\">Time.H Header File In C With Examples<\/h2>\n\n\n\n<p>The <code>time.h<\/code> header file in C contains functions for obtaining and manipulating date and time information. It introduces three time-related data types:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>clock_t<\/code>:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Represents the processor time used by the program as an integer.<\/li>\n\n\n\n<li>It is a part of the calendar time, measuring the CPU time taken for the execution of a program.<\/li>\n<\/ul>\n\n\n\n<p>2.<strong><code>time_t<\/code>:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Represents the calendar time as an integer.<\/li>\n\n\n\n<li>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.<\/li>\n<\/ul>\n\n\n\n<p>3.<strong><code>struct tm<\/code>:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Holds the date and time information in a structured format.<\/li>\n\n\n\n<li>Contains members such as seconds, minutes, hours, day of the month, month, year, and more.<\/li>\n\n\n\n<li>Provides a convenient way to access and manipulate individual components of a date and time.<\/li>\n<\/ul>\n\n\n\n<p>In summary, the <code>time.h<\/code> header file facilitates time-related operations in C programming, offering data types like <code>clock_t<\/code> and <code>time_t<\/code> for specific time representations, and <code>struct tm<\/code> for a structured approach to handling date and time information.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct tm {\n    \/\/ seconds,  range 0 to 59\n    int tm_sec;\n \n    \/\/ minutes, range 0 to 59\n    int tm_min;\n \n    \/\/ hours, range 0 to 23\n    int tm_hour;\n \n    \/\/ day of the month, range 1 to 31\n    int tm_mday;\n \n    \/\/ month, range 0 to 11\n    int tm_mon;\n \n    \/\/ The number of years since 1900\n    int tm_year;\n \n    \/\/ day of the week, range 0 to 6\n    int tm_wday;\n  \/\/ day in the year, range 0 to 365\n    int tm_yday;\n \n    \/\/ daylight saving time\n    int tm_isdst;\n}<\/code><\/pre>\n\n\n\n<p>This will have a CLOCKS_PER_SEC macro that will contain the number of times the system clock ticks per second.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"pre-defined-functions-in-time-h\">Pre-defined functions in time.h<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>S.No<\/th><th>Function Name<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td>1.<\/td><td>asctime()<\/td><td>This function will  returns the date and time in the format&nbsp;<br>day month date hours:minutes:seconds year.<br>Eg: Sat Jul 27 11:26:03 2019.<br>asctime() function returns a string by taking struct tm variable as a parameter.<\/td><\/tr><tr><td>2.<\/td><td>clock()<\/td><td>This function will returns the processor time consumed by a program<\/td><\/tr><tr><td>3.<\/td><td>ctime()<\/td><td>ctime() will return the date and time in the format<br>day month hours:minutes:seconds year<br>Eg: Sat Jul 27 11:26:03 2019<br>time is printed based on the pointer returned by Calendar Time<\/td><\/tr><tr><td>4.<\/td><td><a href=\"https:\/\/www.geeksforgeeks.org\/difftime-c-library-function\/\" rel=\"nofollow noopener\" target=\"_blank\">difftime()<\/a><\/td><td>This function returns the difference between the time.<\/td><\/tr><tr><td>5.<\/td><td><a href=\"https:\/\/www.geeksforgeeks.org\/gmtime-function-in-c-c\/\" rel=\"nofollow noopener\" target=\"_blank\">gmtime()<\/a><\/td><td>This function will print the UTC (Coordinated Universal Time) Time and date.<br>The format for both gmtime() and asctime() is the same.<\/td><\/tr><tr><td>6.<\/td><td>mktime()<br><\/td><td>This function will print the UTC (Coordinated Universal Time) Time and date.<br>Format for both gmtime() and asctime() is the same.<\/td><\/tr><tr><td>7.<\/td><td>time()<br><\/td><td>This mktime() will returns the calendar-time equivalent using struct tm.<\/td><\/tr><tr><td>8.<\/td><td>strftime()<br><\/td><td>This  strftime() will   format the string returned by other time functions using different format specifiers<br><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-program-to-print-the-date-and-time-of-the-system\">1.Program to print the date and time of the system<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\nint main(void)\n{\n    struct tm* ptr;\n    time_t t;\n    t = time(NULL);\n    ptr = localtime(&amp;t);\n    printf(\"%s\", asctime(ptr));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Tue Aug  6 09:00:29 2019\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-program-to-print-utc-coordinated-universal-time-of-the-system\">&nbsp; 2. Program to print UTC (Coordinated Universal Time) of the system<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\nint main(void)\n{\n    struct tm* ptr;\n    time_t t;\n    t = time(NULL);\n    ptr = gmtime(&amp;t);\n    printf(\"%s\", asctime(ptr));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Tue Aug  6 09:00:31 2019<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"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\">&nbsp;&nbsp; &nbsp;3. Program to calculate the time taken to add two numbers program.&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong>Note:<\/strong>&nbsp;If user gives input slowly that time also add up for total execution time.&nbsp;<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\nint main(void)\n{\n    time_t start, end;\n    start = time(NULL);\n    int a, b;\n    scanf(\"%d %d\", &amp;a, &amp;b);\n    printf(\"Sum of %d and %d is %d\\n\",\n           a, b, a + b);\n    end = time(NULL);\n    printf(\"Time taken to print sum is %.2f seconds\",\n           difftime(end, start));\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum of 4196144 and 0 is 4196144\nTime taken to print sum is 0.00 seconds<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-program-to-find-the-clock-ticks\">4. Program to find the clock ticks<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n#include &lt;math.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\n \nint frequency_of_primes(int n)\n{\n    \/\/ This function checks the number of\n    \/\/ primes less than the given parameter\n    int i, j;\n    int freq = n - 1;\n    for (i = 2; i &lt;= n; ++i)\n        for (j = sqrt(i); j &gt; 1; --j)\n            if (i % j == 0) {\n                --freq;\n                break;\n            }\n    return freq;\n}\n \nint main()\n{\n    clock_t t;\n    int f;\n    t = clock();\n    f = frequency_of_primes(9999);\n    printf(\"The number of primes lower\"\n           \" than 10, 000 is: %d\\n\",\n           f);\n    t = clock() - t;\n    printf(\"No. of clicks %ld clicks (%f seconds).\\n\",\n           t, ((float)t) \/ CLOCKS_PER_SEC);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The number of primes lower than 10, 000 is: 1229\nNo. of clicks 2837 clicks (0.002837 seconds).<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-program-to-print-time-as-hour-minute-returned-by-asctime-file\">5. Program to print time as hour: minute returned by asctime() file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\nint main()\n{\n    time_t rawtime;\n    struct tm* timeinfo;\n \n    \/\/ Used to store the time\n    \/\/ returned by localetime() function\n    char buffer&#91;80];\n \n    time(&amp;rawtime);\n    timeinfo = localtime(&amp;rawtime);\n    strftime(buffer, 80,\n             \"Time is %I:%M%p.\",\n             timeinfo);\n \n    \/\/ strftime() function stores the\n    \/\/ current time as Hours : Minutes\n    \/\/%I %M and %p-&gt; format specifier\n    \/\/ of Hours minutes and am\/pm respectively*\/\n \n    \/\/ prints the formatted time\n    puts(buffer);\n  return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Time is 09:00AM.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-time-h-header-file-in-c-with-examples\">FAQ- Time.H Header File In C With Examples<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1700645761062\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the header for time in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The <code>time.h<\/code> header file in C and C++ helps with handling time and date operations. It provides functions and structures to work with time-related tasks.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700645770556\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is time_t structure in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The <code>time.h<\/code> header file in C and C++ helps with handling time and date operations. It provides functions and structures to work with time-related tasks.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700645781226\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is header file in C with example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>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 <code>#include<\/code> 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<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>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: 2.time_t: 3.struct tm: 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, &#8230; <a title=\"Time.H Header File In C With Examples\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/time-h-header-file-in-c-with-examples\/\" aria-label=\"More on Time.H Header File In C With Examples\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5594,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[605],"class_list":["post-3679","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-interview-prep","tag-time-h-header-file-in-c-with-examples","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3679"}],"version-history":[{"count":11,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3679\/revisions"}],"predecessor-version":[{"id":7955,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3679\/revisions\/7955"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5594"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}