File Inclusion
- The second preprocessor directive is file inclusion.
- This directive causes one file to be included in another.
- The general command for file inclusion is:
1#include "filename"
- It is a command given by programmer to preprocessor to include header file in the source program.
- This used when we have a very large program, the code is best divided into several different files, each containing a set of related functions.
- There are some functions and some macro definitions that we need almost in all programs.
- These commonly use functions can be stored in a file, and that file can be included in every program.
There are two ways to write #include statement.
- #include “filename”
- #include <filename>
Each header file ended with .h extension.
.h extension stands for ‘header file’ that contain function declarations which we can be used in our main C program.
The prototypes of all the library functions are grouped into different categories and then stored in different header files.
For example:
- math.h – prototypes of all mathematics related functions are stored in the header file ‘math.h’.
- conio.h – prototypes of console input/output functions are stored in the header file ‘conio.h’.
- stdio.h – prototypes of printf(), scanf() functions are stored in the header file ‘stdio.h’.
It is also possible to create your own header files in which you can declare your own functions and use in your main program.
For example:
1 |
#include "myheader.h" |
This line tells preprocessor to get myheader.h from the local directory and add the content to the current source file.
There are some rules for creating own header files.
It can be start with number also,
1 |
#include "1abc.h" |
It is not allowed to use space in between name of header file,
1 |
#include "a bc.h" |
following defination of header ia also not allowed.
1 |
#include "abc.h.h" |