Updated July 7, 2023
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 included and why it is 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, including C, files with specific functions or operations, whether they are user-defined or system files, are often kept separate for better organization and accessibility. When needed, these files are included in the respective code file. In C, the inclusion of these files is achieved using the #include directive, which is typically placed at the beginning of the code.
Syntax of #include in C
Header files included using the #include directive can be system files or 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 a directory. This is used to include the files that are present in the system directories.
/* Including the system file */
#include <stdio.h>
void main()
{
/* 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 */
#include "firstfile.h"
void main()
{
/* 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. In C programs, the main purpose of header files is to direct the preprocessor to include the content (or code) of the specified header file. Header files typically have a “.h” extension. The preprocessor processes the code of the current (source) file along with the included code from the header file. C allows the nesting of file inclusions using the #include directive.
C uses the above two syntaxes 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. The preprocessor stops once the given file name matches the file that it is looking for. If the compiler cannot find the specified path or file name in a local or standard folder, it will throw an error on the console. The compiler does not consider comments inside the #include statement; it treats them as regular text and starts searching for the file using the provided file name.
Examples of #include in C
Given below are the examples mentioned :
Example #1
Inclusion of system file using the #include <>.
Code:
// Inclusion of standard 'stdio.h' file
#include <stdio.h>
void main()
{
printf("Hello we are learning the #include preprocessor directive");
}
Output:
Explanation:
- 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 that is present in the stdio.h header file, so there would be no error in the execution of the above program. If we did not include the above ‘stdio. h’ file, the compiler would throw an error of the missing function definition.
Example #2
Inclusion of user-defined file using the #include ” “.
File : new_file.h (user defined file)
Code:
// defining the functions in the header file
void printing()
{
printf("\n hello I am in new_file header file");
}
void working()
{
printf("\n hello I used to work in header file");
}
File: header_learning.c
Code:
// including the system file using <> brackets in order to use printf function
#include <stdio.h>
// including the user defined header file created above
#include "new_file.h"
void main()
{
// calling of functions of header file
printing();
working();
printf("\n hello I am in main/current file");
}
Output:
Explanation:
- 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.
Conclusion
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.
Recommended Articles
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 –