{"id":82794,"date":"2021-07-24T09:00:07","date_gmt":"2021-07-24T03:30:07","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=82794"},"modified":"2021-07-24T09:00:07","modified_gmt":"2021-07-24T03:30:07","slug":"header-files-in-c","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/","title":{"rendered":"Header Files in C"},"content":{"rendered":"<p>C programming language offers various exciting and useful features and functionalities to the programmers. Almost every programmer uses the C language to develop software, games, and many others.<\/p>\n<p>Syntax of the C is very easy to understand and also C is easy to implement. In all C programs, Header files play an important role. You cannot compile or execute any programs without specifying a header file.<\/p>\n<p>Let us learn about Header files in C.<\/p>\n<h3>What is a Header File in C?<\/h3>\n<p>A header file is a file that has the .h extension. In C, all header files must have the .h extension. A header file contains:-<\/p>\n<ul>\n<li>Function Declaration<\/li>\n<li>Macros<\/li>\n<li>Data Type Definitions<\/li>\n<\/ul>\n<p>With the help of header file, you can use the above mentioned features in your program code. Before using, you have to include the header file into your program and you can do this with the help of a preprocessor directive called <strong>#include.<\/strong><\/p>\n<p>These preprocessor directives tell the compiler that before compilation, the header files need to be processed. You have to <strong>stdio.h<\/strong> header file in each and every C program code.<\/p>\n<p>The &lt;stdio.h&gt; header file is used for standard input and output operations such as reading data from the user using <strong>scanf()<\/strong> function and printing output on the screen using <strong>printf()<\/strong> function.<\/p>\n<h3>Types of Header Files in C<\/h3>\n<p>There are 2 types of header files such as:-<\/p>\n<ul>\n<li><strong>Standard Library Header Files:-<\/strong> These are pre-existing header files which are available in C.<\/li>\n<li><strong>User-Defined Header Files:-<\/strong> The header files which are defined by the user then they are called user-defined header files. The #define directive is used to define a header file.<\/li>\n<\/ul>\n<h3>Syntax of Header File in C<\/h3>\n<p>There are two ways to include a header file in your program:-<\/p>\n<ul>\n<li><strong>#include&lt;headerFilename&gt;<\/strong><\/li>\n<\/ul>\n<p>The header file is enclosed within angular brackets. This is the most common way of defining a header file.<br \/>\n<strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;time.h&gt;<\/pre>\n<ul>\n<li><strong>#include\u201cheaderFilename\u201d<\/strong><\/li>\n<\/ul>\n<p>This is enclosed within double-quotes. This way, you can define user-defined header files.<br \/>\n<strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include\"stdlib.h\"<\/pre>\n<p><strong>NOTE:-<\/strong> You cannot include the same header file in the same program twice.<\/p>\n<h3>Working of C header files:-<\/h3>\n<p>With the help of <strong>#include<\/strong> preprocessor directive, you can include a header file in the program code. This preprocessor directive tells the compiler that the header file needs to be processed before compilation.<\/p>\n<p>It contains all the important function declarations, macro definitions, and data type definitions.<\/p>\n<h3>Include Operation in C:-<\/h3>\n<p>Let\u2019s say that you have a header file that contains the following declaration.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">char *testing();<\/pre>\n<p>Below is the main.c program which uses this header file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include\"header.h\"  \nint main()  {  \nputs(testing());  \nreturn 0;  \n}<\/pre>\n<p>The compiler will change the definition of the header.h file as below:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">char *testing();\nint main () {\nputs (testing());\nreturn 0;\n}<\/pre>\n<h3>Once Only headers in C:-<\/h3>\n<p>If you include the header file in a program code twice then it will give you an error. So, to avoid this type of problem, you will have to enclose the contents of the header file in a conditional like below:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#ifndef HEADER_FILE\n#define HEADER_FILE\n....\n....\n#endif<\/pre>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#if header\n        #include \"myheader.h\"\n#elif header2\n        #include \"myheader2.h\"\n#elif header3\n   ....\n#endif<\/pre>\n<p>In the above, if the header file is not included then it will be included but if it is included then it won\u2019t be included again. The #ifndef construct will result in false if a header file is included twice.<\/p>\n<p><strong>Example:- Only once headers<\/strong><br \/>\n<strong>header.h<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#ifndef header_file  \n#define header_file  \nint x = 23;  \n#endif<\/pre>\n<p>Below is the source code which uses the header file:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;  \n#include \"header.h\"  \n#include \"header.h\"  \nint main()  \n{\nprintf(\"TechVidvan Tutorial: Header files in C!\\n\");\nprintf(\"Value of x: %d\", x);  \nreturn 0;  \n}<\/pre>\n<p>The above example will not throw an error because we have specified the <strong>ifndef<\/strong> construct. With the help of <strong>ifndef,<\/strong> If the header file is included then it won\u2019t be included again.<\/p>\n<h4>How to create your own header file:-<\/h4>\n<p>Apart from the pre-existing header files in C, you can create your own header file too. With the help of header files, you can avoid writing long and complex codes in your program. It also enhances the readability and functionality of the program code.<\/p>\n<p>Let us take an example on how to create your own header file through steps.<\/p>\n<p><strong>Step:-1<\/strong><\/p>\n<p>Write your own code in the <strong>.h<\/strong> extension file. Because you are creating a header file which you will use in your program code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int fact(int num)\n{\n  int count, fact=1;\n  for(count=1; count&lt;=num; count++)\n  {\n   \t fact=fact*count;\n  }\n  return fact;\n}<\/pre>\n<p>Name the above code as <strong>fact.h.<\/strong><\/p>\n<p><strong>Step:-2<\/strong><\/p>\n<p>Include the <strong>fact.h<\/strong> header file in your program code. You can do this in two ways:-<\/p>\n<ul>\n<li><strong>#include&lt;fact.h&gt;<\/strong><\/li>\n<li><strong>#include\u201dfact.h\u201d<\/strong><\/li>\n<\/ul>\n<p><strong>Step:-3<\/strong><\/p>\n<p>Then compile and run the program code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n#include\"fact.h\"\nint main()\n{\nprintf(\"TechVidvan Tutorial: Create your own header file!\\n\");\nint n=6;\nprintf(\"The factorial of %d is: %d!\",n,fact(6))\nreturn 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: Create your own header file!<br \/>\nThe factorial of 6 is: 720!<\/div>\n<h3>Different header files in C:-<\/h3>\n<p>You can use these standard C library functions by declaring header files. Different header files include different functions and different operations.<br \/>\nBelow is the list of header files in C:-<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Header File<\/b><\/td>\n<td><b>What it does<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">stdio.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Mainly used to perform input and output operations like print(), scanf().<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">string.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Mainly used to perform string handling operations like strlen(), strcmp() etc.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">conio.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">With this header file, you can execute console input and output operations.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">stdlib.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Mainly used to perform standard utility functions like malloc(), calloc() etc.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">math.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Mainly used to perform mathematical operations like sqrt(), pow() etc.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">ctype.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to perform character type functions like isdigit(), isalpha() etc.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">time.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to perform operations related to date and time.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">assert.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to verify assumptions made by a program and print a message if the assumption is false using functions like assert() etc.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">locale.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Mainly used to perform localization like functions such as setlocale() etc.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">signal.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Mainly used to perform signal handling functions such as signal(), raise() etc.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">setjmp.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to perform jump functions.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">stdarg.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to perform standard argument functions like va_start() etc.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">errno.h<\/span><\/td>\n<td><span style=\"font-weight: 400\">For performing error handling operations such as errno() to indicate the errors.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example:- Header files in C<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;  \n#include &lt;string.h&gt;  \n#include &lt;math.h&gt;  \nint main()  \n{  \n  char s1[20] = \"TechVidvan\";  \n  char s2[15] = \"Tutorial\";  \n  printf(\"TechVidvan Tutorial: Example of header files in C!\\n\\n\");\n  int num = pow(4, 2); \/\/ pow() function from math.h header file!\n  printf(\"Value of num: %d\\n\", num);  \n  int len = strlen(s1); \/\/ strlen() function from string.h header file!\n  printf(\"String s1\u2019s length is: %d\", len);  \n  return 0;  \n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">\n<p>TechVidvan Tutorial: Example of header files in C!<\/p>\n<p>Value of num: 16<br \/>\nString s1\u2019s length is: 10<\/p>\n<\/div>\n<p>In the above example, we have included two header files such as <strong>math.h<\/strong> and <strong>string.h.<\/strong> And with the help of these files, we did some operations on the variables like length of the string s1 and calculating the power of the num variable.<\/p>\n<h3>What are Standard library functions in C?<\/h3>\n<p>These functions are also known as built-in functions in C. And these functions are stored in a common collection called a library. Each function provides a different operation.<\/p>\n<p>With the help of these standard functions, a programmer can get the desired output of his\/her program easily. During the time of designing compilers, these library functions are created.<\/p>\n<h3>Summary<\/h3>\n<p>In this tutorial, we learnt about what C header files are and how you can use it in your program code. We discussed the types of header files in C. We also talked about the working of header files.<\/p>\n<p>Then we discussed how you can create your own header files. We discussed different header files in C. Without a header file, you cannot run or compile your program code. In C, all program codes must include the stdio.h header file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C programming language offers various exciting and useful features and functionalities to the programmers. Almost every programmer uses the C language to develop software, games, and many others. Syntax of the C is very&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":82906,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3510],"tags":[3860,3725,3861],"class_list":["post-82794","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-header-files","tag-header-files-in-c","tag-standard-library-functions-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Header Files in C - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about C header files &amp; how you can use them in your program code. See types of header files &amp; their working.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Header Files in C - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about C header files &amp; how you can use them in your program code. See types of header files &amp; their working.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-24T03:30:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Header-Files-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Header Files in C - TechVidvan","description":"Learn about C header files & how you can use them in your program code. See types of header files & their working.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Header Files in C - TechVidvan","og_description":"Learn about C header files & how you can use them in your program code. See types of header files & their working.","og_url":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-24T03:30:07+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Header-Files-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Header Files in C","datePublished":"2021-07-24T03:30:07+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/"},"wordCount":1177,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Header-Files-in-C.jpg","keywords":["C header files","Header files in C","Standard Library Functions in C"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/","url":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/","name":"Header Files in C - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Header-Files-in-C.jpg","datePublished":"2021-07-24T03:30:07+00:00","description":"Learn about C header files & how you can use them in your program code. See types of header files & their working.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Header-Files-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Header-Files-in-C.jpg","width":1200,"height":628,"caption":"Header Files in C"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/header-files-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Header Files in C"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=82794"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82794\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/82906"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=82794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=82794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=82794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}