C Preprocessor Directive / Header Files

#include Directive / Header Files

The #include directive tells the compiler to read another external source file in addition to the one that contains the #include directive.

The name of the source file must be enclosed between double quotes or angle brackets.

For example,
#include "stdio.h"
#include <stdio.h>

both tells the compiler to read the header file ‘stdio.h’ to include the I/O system library functions.

This header file ‘stdio.h’ can have #include directives in their source code , this is referred to as Nested Includes.

The number of levels of nesting may vary for different compilers.

Syntax:
#include <filename>
or
#include "filename"

The basic work of include directive is to import any external file to the source code of C program. To Learn C Language, first you should understand the flow of execution. Let’s start with #include directives in depth.

1. #include <filename>

The above statement will search the specified file in ‘include’ directory (The default location of include directory in TURBO C 3.0 is c:\tc\include).

If the specified file is present then #include statement is replaced by the content of specified file otherwise it will report compilation error.

2. #include”filename”

The above statement will first search the specified file in the current working directory(CWD), if it is not present in CWD then it will search the include directory ,if the specified file is not present in include directory then it causes Compilation Error with an error message ‘unable to open the file’.

Examples of #include <filename>

#include <stdio.h>
#include <conio.h>
void main(){
int x=10;
printf("How are you");
printf("%d",x);
getch();
}

In this example , first two lines will replaced by content of header files stdio.h and conio.h respectively. The above C source code has total 8 lines but when the code is compiled then these two header files adds their content and this 8 lines code becomes much larger as Intermediate file.

Note: We can specify the absolute path of the file in #include statement. In this case , the file will search only to the absolute path. If the file is exist there then #include statement is replaced by its content otherwise ‘Compilation Error’ comes.C PREPROCESSOR DIRECTIVE INCLUDE HEADER FILES

Example:
Step1:
Save the below code as externalfile.c at the path C:\tc\bin\test
int add(int a,int b)
{
return a+b;
}
Step 2:
Save the below code as mainfile.c:
#include<C:\tc\include\stdio.h>
#include<c:\tc\include\conio.h>
#include<c:\tc\bin\test\externalfile.c>
void main()
{
int x;
x=add(10,20);
printf("%d",x);
getch();
}
Output :30
The above code can be saved in any directory or drive and code will compiled and run successfully without any error because we specify the absolute path to each directive in #include statement.

Examples of #include”filename”

There are 4 cases:

  1. File is present in CWD (current working directory)
  2. File is not present in CWD (current working directory) but present in ‘include’ directory
  3. File is present in both Current working directory and ‘include’ directory
  4. File is not present in both Current working directory and ‘include’ directory

Case1 Example:
Suppose your current working directory is: C:\tc\bin and below test1.c is saved at this path.

test1.c (CWD is: C:\tc\bin)
int add(int a,int b)
{
return a+b;
}
test2.c (Its by default path is: C:\tc\bin)
#include"stdio.h"
#include"conio.h"
#include"test1.c"
void main()
{
int x;
x=add(10,20);
printf("%d",x);
getch();
}
Output :30
Case2 Example:
Suppose the file is not present in Current working directory and below test1.c is saved in 'include' directory.

test1.c (CWD is: C:\tc\include)
int add(int a,int b)
{
return a+b;
}
test2.c (Its by default path is: C:\tc\bin)
#include"stdio.h"
#include"conio.h"
#include"test1.c"
void main()
{
int x;
x=add(10,20);
printf("%d",x);
getch();
}
Output :30
Case3 Example:
Suppose the file is present in both current working directory(C:\tc\bin) and include directory.

test1.c (CWD is: C:\tc\bin & also in C:\tc\include)
int add(int a,int b)
{
return a+b;
}
test2.c (Its by default path is: C:\tc\bin)
#include"stdio.h"
#include"conio.h"
#include"test1.c"
void main()
{
int x;
x=add(10,20);
printf("%d",x);
getch();
}
Output :30
Case4 Example:
Suppose the file is not present in both current working directory (C:\tc\bin) and include directory.

test1.c
int add(int a,int b)
{
return a+b;
}
test2.c (Its by default path is: C:\tc\bin)
#include"stdio.h"
#include"conio.h"
#include"test1.c"
void main()
{
int x;
x=add(10,20);
printf("%d",x);
getch();
}
Error: Compilation error , unable to open test1.c

Important: In turbo c 3.0 compiler it is not necessary to include stdio.h and conio.h if you have saved the file with .c extension.

In turbo C 3.0 version compiler , If you do not include stdio.h & conio.h in a c program saved with .c extension , then still the program will successfully compiled and run because some header files like stdio.h & conio.h are automatically included when a C program is compiled.

Leave a Reply

Your email address will not be published. Required fields are marked *