Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is the #include directive in C/C++?

Samia Ishaque

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The #include directive in C is interpreted by the preprocessor to include the header file’s content, and can be used to include code from user-defined or compiler-based header files into a program. The #include directive is written at the beginning of a file to ensure that all header files are included before compiling the rest of the code. There are two ways that this directive can be used:

1) Compiler based header files

Compiler based header files contain standard library functions that can be used by a developer to help write their program. These files include the '.h' extension at the end and are written within <> brackets.

For example, the following code demonstrates that including 'stdio.h' enables you to use the printf() function for printing outputs onto a screen:

#include <stdio.h>
int main() {
// your code goes here
printf("hello world");
return 0;
}

Additionally, the C language has many other library functions that can be implemented through the use of header files. Some common examples are:

  1. The <iostream> header file can be used to receive input using cin>>, and print output of a program using cout<<.

  2. <time.h> can be used to retrieve the duration of time in between two events using the time() function.

  3. <string.h> can be used for string manipulation using functions like strlen() to find the length of a string, strcpy() to copy one string into another, and strcat() to append one string to another.

  4. <math.h> is a very important header file as it includes functions such as pow(x,y) to raise x to the power of y, sin(x), and cos(x).

2) User defined header files

User defined header files are defined by the user beforehand and are used in large projects where breaking code down into files with helper functions helps to better organize the code. When the compiler runs the program, it searches for this file in its own directory to include it in the code. These files have the '.h' extension at the end and are written within quotation marks "".

For example, the following code adds two numbers together by including the user-defined 'adder.h'a function that performs specific tasks for the user file. The user can then input two numbers as parameters into the adder function, and the sum will be printed on the screen.

main.c
adder.h
#include <stdio.h>
void adder(int a, int b){
int c = a + b;
printf("Your sum is: %d\n", c);
}

The difference between compiler based header files and user defined header files lies in the syntax you use (i.e., "" or <>).

It is important to use the correct syntax in both cases.

The use of "" indicates that the mentioned header file is available in the same directory and the compiler can look for it. However, it is imperative that the header files are named correctly, both in the directory as well as within the code.

The use of <> indicates that the compiler must search for the header file in its pre-defined header files.

RELATED TAGS

c

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring