Q2 What is File Inclusion in C Preprocessors? Why we need file inclusion in our program?
Program: 146
What is C Preprocessor? Types of C Preprocessors? Marco expansion, File Inclusion, Conditional Compilation, Miscellaneous Directives. What is File Inclusion in C Preprocessors? Why we need file inclusion in our program?
Definition:
It is a program that processes our source program before it is passed to the compiler. (C Preprocessor commands also known as directives)
C PREPROCESSOR
There are four types of directives exists in C preprocessor
- Macro expansion
- File Inclusion
- Conditional Compilation
- Miscellaneous directives
File Inclusion
This type of preprocessor directives insert the entire contents of file into the source program.
We have two cases where we use the file inclusion
- For large program where we divide it into the smaller programs (files)
- If similar functions and macro definitions are using frequently
1. Header files or standard files: These files contains definition of pre-defined functions like printf(), scanf() and sqrt() so on stored in stdio.h and math.h header files.
syntax: #include <file_name>
The ‘<‘ and ‘>’ brackets tells the compiler to look for the file in standard directory.
2. User Defined files: When a program becomes very large, it is good practice to divide it into smaller files and include whenever needed. These types of files are user defined files.
syntax: #include “file_name“
It tells compiler to look for the file in specified directory. If path is not defined with file name then it will search for that file in the same directory where the main program file exists. Also we define multiple path location separated by semicolon (;).
For example: c:\Program File\Codeblock\lib; c:\Program File\Codeblock\mylib
Example:
Output:
50.27
circle.c
This file has definition of the function/methods.
circle.h
This file has all the macro definitions and function declaration (prototypes).
L8Q2.c
This is our main C program where we include circle.h file. By including this file we also include macro definition for PI and also the function to calculate area of circle.
Note: Make sure L8Q2.c, circle.c and circle.h are all in the same directory/folder or you defined a proper path for all of them.