How to write your own header file in C? Last Updated : 24 Oct, 2017 Comments Improve Suggest changes Like Article Like Report As we all know that files with .h extension are called header files in C. These header files generally contain function declarations which we can be used in our main C program, like for e.g. there is need to include stdio.h in our C program to use function printf() in the program. So the question arises, is it possible to create your own header file? The answer to the above is yes. header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs. NOTE:Header files generally contain definitions of data types, function prototypes and C preprocessor commands. Below is the short example of creating your own header file and using it accordingly. Creating myhead.h : Write the below code and then save the file as myhead.h or you can give any name but the extension should be .h indicating its a header file. C // It is not recommended to put function definitions // in a header file. Ideally there should be only // function declarations. Purpose of this code is // to only demonstrate working of header files. void add(int a, int b) { printf("Added value=%d\n", a + b); } void multiply(int a, int b) { printf("Multiplied value=%d\n", a * b); } Including the .h file in other program : Now as we need to include stdio.h as #include in order to use printf() function. We will also need to include the above header file myhead.h as #include"myhead.h". The " " here are used to instructs the preprocessor to look into the present folder and into the standard folder of all header files if not found in present folder. So, if you wish to use angular brackets instead of " " to include your header file you can save it in the standard folder of header files otherwise. If you are using " " you need to ensure that the header file you created is saved in the same folder in which you will save the C file using this header file. Using the created header file : C // C program to use the above created header file #include <stdio.h> #include "myhead.h" int main() { add(4, 6); /*This calls add function written in myhead.h and therefore no compilation error.*/ multiply(5, 5); // Same for the multiply function in myhead.h printf("BYE!See you Soon"); return 0; } Output: Added value:10 Multiplied value:25 BYE!See you Soon NOTE : The above code compiles successfully and prints the above output only if you have created the header file and saved it in the same folder the above c file is saved. Important Points: The creation of header files are needed generally while writing large C programs so that the modules can share the function definitions, prototypes etc. Function and type declarations, global variables, structure declarations and in some cases, inline functions; definitions which need to be centralized in one file. In a header file, do not use redundant or other header files; only minimal set of statements. Don’t put function definitions in a header. Put these things in a separate .c file. Include Declarations for functions and variables whose definitions will be visible to the linker. Also, definitions of data structures and enumerations that are shared among multiple source files. In short, Put only what is necessary and keep the header file concised. This article is merely to give you idea about the creation of header files and using the same but this is not what actually happens when you write a large C program. The creation of header files are needed generally while writing large C programs so that the modules can share the function definitions, prototypes etc. Comment More infoAdvertise with us Next Article Macros and its types in C D Dimpy Varshni Improve Article Tags : Misc C Language File Handling Practice Tags : Misc Similar Reads C Preprocessors Preprocessors are programs that process the source code before the actual compilation begins. They are not part of the compilation process but operate separately, allowing programmers to modify the code before compilation. It is the first step that the C source code goes through when being converted 8 min read C Preprocessor Directives In C programming, the preprocessor is a program that process the source code before the actual compilation begins. It uses preprocessor directives are commands that instruct the preprocessor to perform specific actions. These directives start with the # symbol.List of Preprocessor DirectivesThe foll 6 min read How a Preprocessor works in C? Compiling a C program - Behind the Scene A Preprocessor is a system software (a computer program that is designed to run on computer's hardware and application programs). It performs preprocessing of the High Level Language(HLL). Preprocessing is the first step of the language processing system. Lan 3 min read Header Files in C In C programming, a header file is a file that ends with the .h extension and contains features like functions, data types, macros, etc that can be used by any other C program by including that particular header file using "#include" preprocessor.C language uses header files to provide the standard 5 min read Whatâs difference between header files "stdio.h" and "stdlib.h" ? In C programming, standard header files provide various inbuilt functionalities and two of the most commonly used standard header files are stdio.h and stdlib.h. The <stdio.h> provides Standard Input Output tools such as printf(), scanf(), etc while <stdlib.h> provides some commonly used 4 min read How to write your own header file in C? As we all know that files with .h extension are called header files in C. These header files generally contain function declarations which we can be used in our main C program, like for e.g. there is need to include stdio.h in our C program to use function printf() in the program. So the question ar 4 min read Macros and its types in C In C programming, a macro is a symbolic name or constant that represents a value, expression, or code snippet. They are defined using the #define directive, and when encountered, the preprocessor substitutes it with its defined content.ExampleC#include <stdio.h> // Macro definition #define LIM 4 min read Interesting Facts about Macros and Preprocessors in C In a C program, all lines that start with # are processed by preprocessor which is a special program invoked by the compiler. by this we mean to say that the â#â symbol is used to process the functionality prior than other statements in the program, that is, which means it processes some code before 5 min read # and ## Operators in C In C, # and ## operators are preprocessor operators using in macros for token manipulation. They are known as stringizing and token pasting operators and are used in macro definition with #define preprocessor. In this article, we will learn about these operators and how to use them in C programs.Str 3 min read How to print a variable name in C? Printing a variable name means printing the identifier that is assigned to the variable. To print it, it should be in the form of string. This can be done by using stringification.Stringification in C is the method to convert the argument of a function like macro to a string. This can be done with t 1 min read Like