Let Us Explore #include in C and C++ Programming - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Let Us Explore #include in C and C++ Programming

Share This


#include in C or C++ source codes represents pre-processor directives. In general, it includes header files with .h extensions so that we can use the definition of the predefined function which is already there in the corresponding header files. But in actuality, it can include any sort of file beyond the scope of header files only.


In general, we are quite happy with writing it in the form of #include and seldom we put more concentration in knowing the details of it. 

Here in this tutorial,, we are going to explore #include in details


Formats of #include


There are two formats of #include as shown below.
  1. #include<filename.h>; and
  2. #include"filename.h"
#include<filename.h> will always search for the header file with the name filename in the standard system directories and if found, will include that to resolve function calls in the source code

On the other hand, #include"filename.h" will search for the header file with the name filename, first in the current directory and if failed, next in standard system directories. 

In general this format is used to include header files associated with user defined functions.


#include Nesting


#include can be nested. Let us suppose we are using #include such that in turn bunksallowed.h includes div.h

In this case, while in the main function, we are including bunksallowed.h, at the same time, the dependent div.h is also getting included. 



Let's discuss nesting #include directives with an example. Suppose you have three header files: main.h, math_functions.h, and constants.h. Here's how they might be structured: 
 

constants.h

#ifndef CONSTANTS_H #define CONSTANTS_H #define PI 3.14159265359 #define SPEED_OF_LIGHT 299792458 // meters per second #endif

math_functions.h

// math_functions.h #ifndef MATH_FUNCTIONS_H #define MATH_FUNCTIONS_H #include "constants.h" // Nested include double circle_area(double radius); #endif

main.h

// main.h #ifndef MAIN_H #define MAIN_H #include "math_functions.h" // Nested include #endif
In this example: constants.h contains definitions of mathematical constants like PI and the speed of light. math_functions.h includes constants.h to utilize those constants in its functions. main.h includes math_functions.h, which indirectly includes constants.h due to nesting. Now, let's create a main.c file that includes main.h and uses the functionality provided by the nested headers: 
 
// main.c #include "main.h" #include <stdio.h> int main() { double radius = 5.0; double area = circle_area(radius); printf("Area of the circle with radius %.2f is %.2f\n", radius, area); return 0; }
And finally, let's define the function circle_area in math_functions.c:
// math_functions.c #include "math_functions.h" double circle_area(double radius) { return PI * radius * radius; }
Here's what happens during compilation: 
 
When main.c is compiled, it includes main.h. main.h includes math_functions.h. math_functions.h includes constants.h due to nesting. All necessary declarations and definitions are available to main.c, allowing it to compile successfully.

Using nested #include directives in this way helps to keep your code modular and organized, as each header file can focus on a specific aspect of functionality. However, be cautious not to create unnecessary dependencies or circular includes, as this can lead to compilation issues and make the codebase harder to maintain.

Note of caution: If not all the levels of the nesting are implemented by you, to be on the safer side, please include all the dependent header files explicitly in the main function

Happy Exploring!

No comments:

Post a Comment