Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Importing Files using #include
2.1.
Example
3.
Frequently Asked Questions
3.1.
What does the ‘ #’ symbol mean in preprocessor directives?
3.2.
What is the difference between #include and #include “ ”?
4.
Conclusion
Last Updated: Mar 27, 2024

#include in C

Author Yashesvinee V
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Programming in C is a lot easier using the help of predefined functions. These functions are present in the predefined standard libraries of the C header files. For example, the strlen() function helps avoid the need to use a loop to find the length of a string. This function is present in the string.h header file. Header files need to be imported before any of their functions are put to use. The #include preprocessor directive does this work of importing the contents of a header file into our program.

Also see: C Static Function, and  Tribonacci Series

Importing Files using #include

Preprocessor directives are commands given to the compiler to perform actual compilation. Preprocessor directives are executed before compilation. #include is an important preprocessor directive used to import header files into the program. All header files in C have the ‘.h’ extension. Including a header file in our program gives us access to all its functions and methods.

The #include directive can be used to include two types of header files.

Pre-existing header files: Files already available in the C/C++ compiler.
 

User-defined header files: Files with ‘.h’ extension created by users.
 

The #include preprocessor directive statement goes through the C preprocessors to scan for a specific file and then follows the rest of the existing source file. The # include directive can be used in two different ways. 

#include <header_file.h>

or

#include "header_file.h"

 

The difference between the two syntaxes is important to be noted. The preprocessor searches a header file included within <> in a predetermined directory path. If the header file is enclosed within double quotes, the preprocessor looks for the header file in the same directory as the source file. Note that header files can not be included more than once in a program.

Some of the most frequently used header files are:

Header files

Example

The program shown below uses the sting and math header files to find the length of a string and the cube of a number respectively.

#include<stdio.h>
#include<string.h>
#include<math.h>

int main()
{
    char str[] = "Hello World";
    int length = strlen(str);

    printf("length of string str is %d", length);
    printf("\nThe cube of 10 is %.1f",pow(10,3));
    
    return 0;
}

 

Output:

length of string str is 11
The cube of 10 is 1000.0

 

You can also read about dynamic array in c and Short int in C Programming

Must Read what is storage class in c

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What does the ‘ #’ symbol mean in preprocessor directives?

All preprocessor directives begin with a ‘#’ symbol. This symbol indicates that the statement following it will refer to a program that the preprocessor will execute.

What is the difference between #include and #include “ ”?

Using #include makes the preprocessor search for standard header files in predefined locations. #include “ ” on the other hand, makes the preprocessor search for user-defined header files in the same directory as the source file.

Conclusion

This article extensively discusses preprocessor directives in C. We hope that this blog has helped you enhance your knowledge about the #include preprocessor directive. If you would like to learn more, check out our articles on  Basics of C and Functions in C. Learn more about ArraysPointersMemory managementStructures and Unions in C.

Recommended Reading: include stdio h

Looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc? Have a look at the problems, interview experiences, and interview bundle for placement preparations. Upvote our blogs if you find them helpful and engaging! Happy Coding!

Previous article
C Macros
Next article
C #define
Live masterclass