Preprocessor 2 TYPE - FILE INCLUSION


Preprocessor TYPE 2 - FILE INCLUSION 

As the name itself implies that one file can be included into another. It can be done using,

#include "filename"

the above line includes the contents of file specified in the quotation mark into the program in which this line is written.

Reasons for using :

1. If a program being written becoming very large then it can be splitted into many files which contains set of functions. This concept of writing program supports a characterstic of C language i.e. Structured Programming. This helps the programmer to analyse and debug the program in the future is easy.

2. In the process of software development we get various set of functions which are required frequently in many parts of programs in your whole software development such things can be written in one file and included wherever it is required that save space and time.

Usually to do the above file inclusion we use #include directive, it can be written in two ways,

1)   #include "filename"

The above line looks for the file name included in quotation mark in current directory(folder) 
as well as the specified list of directories as mentioned in the search path which is set in c editor like turbo c etc.

2)  #include
The above line looks for the file name included in quotation mark in the specified list of 
directories as mentioned in the search path which is set in c editor like turbo c etc. Normally the  filename included in the quotation mark in #include always ends with an extension .h stands for 
header file. Normally these files always contain only functions no main() function is written inside them. The main () function written in the program where these #include directives are written.

Example: 
Following program depicts #include directive.


Steps :

1. Create a new file in any c editors
2. Write the following code 
Sample()
{
printf("Wel come to Sridhanvantari.blogspot.com");
printf("#1 C BLOG");
}
3. Save the above program by name a.h
4. Create another new file  in C editor
5. Write the following program,
#include
#include "a.h"
main()
{
Sample();
}
6. Save the above program 
7. Compile and execute
8. Check the output


Video Demonstration using Online C Compiler
(View in full screen for clear viewing)



Comments