Header Files
Header Files
©C
- I’m Programmer
1
Header files contain the set of predefined standard library functions that we can include in
our c programs. But to use these various library functions, we have to include the
appropriate header files.
Let us see in detail how the compiler interprets the line :
#include<stdio.h>
Here, #
is a preprocessor directive which tells us that this is the line which must be
pre-processed by pre-processor.
include tells us that there is a filename ahead which must be included at the top of our
program. Preprocessor simply copies contents of file stdio.h in our code.
<> - Angled brackets defines where to search for header files.
stdio.h - is the file to be included in our program so that we can use built-in functions in
our program. These built-in functions are only declared in such header files and not
defined.
Apart from method or class declarations, header files also contain predefined macros, data
type definitions, etc.
When you call a built-in function, at compile time compiler compares your calling statement
with function prototype(which is in the header file) and if the return type, function name,
number of arguments, type of arguments are same then only the result of comparison is
said to be satisfying otherwise compiler gives you errors.
We can create our own header files as well. But they are specified in between double
quotes instead of angular brackets which will convene to make programming easier.
If you have a standard set of instructions that you want to insert in a lot of programs that
you are writing then you can do it using the # include statement.
2
The # symbol at the start stipulate that this isn't a C statement but one for the C
pre-processor which looks at the text file before the compiler gets it. The # include tells the
pre-processor to read in a text file and treat it as if it was part of the program's text. For
example:
#include "copy.txt"
could be used to include a copyright notice stored in the file c
opy.txt. However, the most
common use of the # include is to define c onstants and macros. The C pre-processor is
almost a language in its own right For example if you define the identifier N ULL as:
#define NULL 0
then whenever you use N ULL in your program the pre-processor substitutes 0. In most
cases you want these definitions to be included in all your programs and so the obvious
thing to do is to create a separate file that you can # include.
This idea of using standard include files has spiraled out of all proportions. Now such
include files are called h eader files and they are distinguished by ending in the extension
.h. A header file is generally used to define all of the functions, variables, and constants
contained in any function library that you might want to use.
There are many header files in C programming language and there all header files have
their own different functionalities…
List of all header file of c language as below.
3
4
Header Files
What's A Header File
A header file is a place to store information that does not exclusively belong in a .c file. Imagine
you have a struct that you want two different .c files to use. Instead of declaring the struct twice, you
can just make a header file with that struct in it and include it in both files.
//#defines
//structs
//prototypes
#endif
//END FILE (once again, not actually in the file)
You probably noticed those #ifndef and #endif. These are to stop something called
“double/multiple inclusion”. That's when a file gets included more than once. It is not necessary,
especially in simpler programs. If you don't understand, than don't worry too much about it.
When you have a simple, single include program, you can compile it just like you would any
other program: “gcc myFile.c”. Notice how myFile.c uses stuff that is in the header file.
Multiple File Use
Whenever you have multiple .c files, things get a bit more complicated. Notice how main only
exists in one of the files. Look how both files needed the myStruct, so they share the header file. Also
notice how myOtherFile.c defines the function “myFunction”, but myFile is still allowed to use it. This
is because they both share the same header file. To compile this you will need to provide both the files
on the command line: “gcc myFile.c myOtherFile.c”. The order of the files doesn't matter.