What is File Inclusion in C?

This C Tutorial explains File Inclusion in C programming.

#include causes the contents of another file to be compiled as if they actually appeared in place of the #include directive. The way this substitution is performed is simple, the Preprocessor removes the directive and substitutes the contents of the named file. Compiler allows two different types of #include’s, first is standard library header include, syntax of which is as follows,

#include <filename.h>

Here, filename.h in angle brackets causes the compiler to search for the file in a series of standard locations specific to implementation. For example, gcc compiler on Linux, searches for standard library files in a directory called /usr/include.

Other type of #include compiler supports is called local include, whose syntax is as follows,

#include "filename.h"

filename in double quotes “” causes compiler to search for the file first in the current directory and if it’s not there it’s searched in the standard locations as usual. Nevertheless, we can write all our #include in double quotes but this would waste compiler’s some time while trying to locate a standard library include file. Though, this doesn’t affect runtime efficiency of program, however, it slows down compilation process. For ex.,

advertisement
advertisement
#include "stdio.h"

Notice here that stdio.h is a standard library file which compiler, first, should required to search in the current directory before locating it in standard location. A better reason why library header files should be used with angle brackets is the information that it gives the reader. The angle brackets make it obvious that

Note: Join free Sanfoundry classes at Telegram or Youtube
#include <strig.h>

references a library file. With the alternate form

#include "string.h"

it’s not clear if string.h is a library header or local file with same name is being used.

advertisement

Remember that header files are suffixed with .h extension by convention. We can write our own header files and include them in programs. For ex.,

/* sll.h */
 
#include <stdio.h>
#include <stdlib.h>
 
#define T 1
#define I 2
#define S 3
#define D 4
#define Q 5
#define TRAVERSE "enter 1 to TRAVERSE the list..."
#define INSERT "enter 2 to INSERT new value in the list..."
#define SEARCH "enter 3 to SEARCH a value in the list..."
#define DELETE "enter 4 to DELETE a value from the list..."
#define QUIT "enter 5 to QUIT the program..."
 
typedef struct NODE {
                     struct NODE *link;
                     int value;
        } Node;
 
/* function declarations */
int traverse(Node **);
void insert(Node **, const int);
int search(Node **, const int);
int delete(Node **, const int);

Notice here that header file named sll.h contained declarations to be used in a particular program. The fact that everything in the header file is compiled each time it’s #include’d suggests that each header file should only contain declarations for one set of functions or data. It’s better to contain several header files, each containing the declarations appropriate for a particular function or module than put up all declarations for the entire program in one giant header file.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

If you find any mistake above, kindly email to [email protected]

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.