Introduction to #include in C
If you have ever worked or studied the C language, you must have seen that the very first line of the code starts with #include directive. Let us see what is #include and why is it used before writing any code in C language. #include is basically a preprocessor directive (as it is read by preprocessor) which is used to involve or include the user or system defined libraries in the below C code. These libraries or the header file names which we want to include in the code are added after the #include directive.
These libraries or header files are present outside the current program. In every programming language, different files either user defined or system files performing having specific functions or operations are kept separately for the easy manageable organization and accessibility and are included in the respective file once required. In C, these files are imported in the code using #include directive by the compiler and are mentioned in the starting of the code.
Syntax of #include in C
Header files included using the #include directive can be system files or the user defined files.
- System files are standard files: These files basically contain the declaration of functions and macros definition that is shared between various source files of C. These files are by default present in the C library (there is no need to create them).
- User-defined files: These files are almost similar to the system or standard files with the only difference being that they are written by the user in order to reduce the repetition or the proper management of code.
Before using the #include directive we need to provide the information to the preprocessor from where to look for the respective header file.
Below given is the basic syntax of using the C #include directive for including both types of files in the code:
1. #include <filename>: While including the file using <>, the preprocessor will search the respective file in the predetermined path of directory. This is used to include the files that are present in the system directories.
/* Including the system file */
/* C code to be written here */
2. #include “filename”: While using the “ “ in including the header file, the preprocessor will look for the included file in the current directory of the source file.
/* Including the user defined file */
/* C code to be written here */
How #include Directive works in C?
There are two types of header files and two ways of including these files using the #include directive in any C program. Header files in C have an extension “.h”. In C programs, the main motive is to direct the preprocessor to the specified header file in order to include its content (or the code written in that file). It works like a simple copy pasting the code of one file to the other file (but it is not done to keep the code manageable and easily accessible) and processing the code of the current (source) file. C allows the nesting of file inclusions using the #include directive.
C uses the above two syntax in order to include the header files in the source code. #include directs the preprocessor to look for the respective file and if there is an incomplete path inside the double quotes, it first looks for the file in the current source file only then in the standard folder. Preprocessor stops once the given file name matches the file which it is looking for. If not found in any case (wrong path or file name) in a local or standard folder, an error is thrown on the console. Comments inside the #include are not considered as it treats it as a normal text and starts looking with that respective file name.
Examples of #include in C
Given below are the examples mentioned :
Inclusion of system file using the #include <>.
// Inclusion of standard ‘stdio.h’ file
printf("Hello we are learning the #include preprocessor directive");
- In the above code we are including the ‘stdio.h’ header file in our code. Preprocessor will first search for the system file ‘stdio.h’ in the standard directory of C header files and once found will include all the code of that file in the current source file before moving forward to the code of print statement in the above code.
- printf() is the predefined function which is present in the stdio.h header file, so there would be no error in the execution of the above program. If we will not include the above ‘stdio.h’ file, the compiler would throw an error of the missing function definition.
Inclusion of user defined file using the #include ” “.
File : new_file.h (user defined file)
// defining the functions in the header file
printf("\n hello I am in new_file header file");
printf("\n hello I used to work in header file");
// including the system file using <> brackets in order to use printf function
// including the user defined header file created above
// calling of functions of header file
printf("\n hello I am in main/current file");
- In the above code, we created one header file with the name ‘new_file.h’ having some functions in it. We have created a main ‘header_learning.c’ file including the above header file using the “ “. On compiling the above program, the preprocessor will first look for the ‘stdio.h’ file in the standard directory of headers and then ‘new_file.h’ header file in the current directory.
- If we need to include the stdio.h using “ “ in the program, we need to ensure that this header file needs to be present in the current directory. Code of both the header files will get included in the main file so on calling both the functions (printing and working) of the header file (new_file.h) and printf function of header file (stdio.h), the program will execute properly without any issue.
Above description clearly explains the #include directive in C and how it is used in programming. Before learning of how to program in C language, the first line of code starts with #include directive. Most of us use it but do not actually know why it is used. Before moving forward it is very important to know every key point in order to have in-depth knowledge. There are some header files including the basic functions which are normally used in every program and hence imported every time.
This is a guide to #include in C. Here we discuss how #include directive works in C along with respective programming examples. You may also have a look at the following articles to learn more –