Why we use #include Directive in C Program?

Question13: Why #include Directive is required in a C Program?

Answer: #include files are called header files as they contain declarations for family of functions, say, printf(), scanf() etc., and declarations for #defined symbols, say EOF (end of file) etc..

As we know that compiler supports two types of #includes in C programs, these are,
1. Standard Library header whose syntax is as follows

#include <filename.h>

angle brackets cause compiler to search for “filename” in a series of standard locations specific to implementation. On Linux system, standard locations include a directory called

/usr/include

2. local includes whose syntax is as follows,

advertisement
advertisement
#include "filename.h"

Notice that double quotes “” cause compiler to search for the “filename” in current directory first, and if file’s not there, it’s searched in standard locations as usual.

ANSI C99 compiler comes with Standard Library Specification. These libraries contain family of functions for specific tasks. For example, printf(), scanf(), gets(), puts() etc., functions perform input and output related tasks so these have been prototyped in stdio.h standard header. Similarly, memmove(), memcpy(), memcmp() etc. functions have been together prototyped in stdlib.h standard header files.

We write programs which perform certain functions. For ex., program accepts a line of input, converts all characters in uppercase and prints the modified line. To accomplish the required task, either we need to write our own functions each time we need for or we have them ready with us in some form. Further, program written for use on one implementation works fine on other implementations too if so required.

Therefore, standard libraries are written which include different sets of functions for different requirements and for different implementations to enhance program’s portability. Just we need to include them in our program whenever we need for them.

This inclusion of standard header in our programs is made possible by preprocessor. Preprocessor performs textual replacement on #directives in the source code. Wherever it finds

#include or #include “filename.h”

it searches the header file in impementation specific locations and once found, replaces the filename with it’s contents into the program as if we had typed in there. Let’s see a simple C program,

advertisement
/* cprog.c */
#inclde <stdio.h>
 
int main(void)
{
    printf("How do you like Programming in C?\n");
    return 0;
}

In order to see the output of preprocessor, type in following cmd on Linux shell prompt,

cpp cprog.c > cprog.i

Here, cpp stands for C preprocessor, cprog.c is file which you want to be processed upon, and ‘>’ operator causes output to send to, say, cprog.i etc.. If cprog.i isn’t already existing ‘>’ will create the file cprog.i and send output into it. Notice that preprocessor output is a file with .i extension. You can view the contents of this file using vim or some other editor. Analyse the output!

advertisement

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.