×

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally reads the #include directive. It then orders to include the contents of the system or a user defined header file within the application or the program.

Most of the files used by including the #include directive is either imported from outside the source of the current program or are defined within the same program or application. This process of importing the files from outside the program might be user defined or system indicated and termed file inclusion.

Such types of preprocessors tell the compiler to include the files within the program’s source code so that the content could be used in future times. In other words, the C standard preprocessor will tell the compiler to include all the contents of the file which are specified in the stream of the input and then continue the same with the rest of the application or program.

The preprocessor will insert the contents of the header file mentioned into the main code from the time it encounters the #include directive. The directives are made to include the C functions or the C header files, which will be held outside the present program.

Syntax:

There are two variants in the #include directive. They are:

#include “user defined file.”
		Or
	#include “header file”

The double quotes are used when the preprocessor has to access the current directory within which the source “header file” or “user defined file” is located. This is mainly used to access any of the header files required for the program or the application or the user-defined, files. It tells the compiler to look for the directory where system header files are held. In UNIX, it is \usr\include directory.

#include <header file>
		Or
	#include <user defined file>

While importing files using angular brackets(<>), the preprocessor uses a predetermined directory path to access the file. It is mainly used to access system header files located in the standard system directories. It tells the compiler to look in the current directory from where the program is running.

Header file: header files are the name of the files which a developer wants to include. This ends with the “.h” and contains declarations and macro definitions that can be shared among several source files. Functions like the printf(), scanf(), cout, cin, and various other input-output or other standard functions are contained within different header files. So to utilize those functions, the users need to import a few header files which define the required functions.

User defined files: these are the types of files that resemble the header files but are written and defined by the user. It saves the user from writing many files a multiple number of times.

The #include directives copy and paste the content of the files into the current file. If the file which needs to be included, is neither system-defined nor user-defined, then the compiler will throw an error. By the use of #include directive, we provide information to the preprocessor where to look for the header files.

The difference between these two syntaxes is subtle but essential. If a header file is included within <>, the preprocessor will search a predetermined directory path to locate the header file. If the header file is enclosed in “ ”, the preprocessor will look for the header file in the same directory as the source file.

E.g.:

#include <stdio.h>
#include <conio.h>
int main()
{
printf("Tutorials and Examples provide you %d + lectures on the basics of programming languages \n", 1000);
return 0;
}

Output:

Tutorials and Examples provide you 1000 + lectures on the basics of programming languages.

In the following example, we are using the #include directive to include the stdio.h header file, which is required to use the printf standard C library function in your application.

Other types of header files in the C standard include:

  1. <assert.h>: it diagnoses the functions
  2. <ctyle.h>: it handles the character handling functions
  3. <locale.h>: It localizes the functions.
  4. <math.h>: it handles the mathematic functions
  5. <signal.h>: it signals the handling functions
  6. <conio.h>: stands for console input and output header file.
  7. <stdlib.h>: these are the general utility functions
  8. <stdarg.h>: it refers to the variable argument list functions
  9. <stdio.h>:  it indicates the Input/Output Functions
  10. <string.h>: it indicates the general string functions within the program
  11. <time.h>: it refers to time and date functions
  • In #include directive, comments are not recognized. So in case of #include <a//b>, a//b is treated as filename.
  • In #include directive, the backslash is considered as normal text, not escape sequence. So in case of #include <a\nb>, a\nb is treated as filename.
  • You can use the only comment after filename otherwise it will give an error.

Related Topics

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.