Do you really understand #include?

The #include directive is used in C programming to include the contents of a header file into the source code during the compilation process. This is a fundamental mechanism for organizing code into modular components and reusing code across multiple source files. Let's take a detailed look at the #include directive with a larger example:

Suppose we have three files:

  1. main.c: The main source file where the program starts.

     // main.c
    
     #include <stdio.h>
     #include "math_operations.h" // Include our custom header file
    
     int main() {
         int a = 10, b = 5;
    
         printf("Sum: %d\n", add(a, b));
         printf("Difference: %d\n", subtract(a, b));
    
         return 0;
     }
    
  2. math_operations.h: A custom header file that declares functions for mathematical operations.

     // math_operations.h
    
     #ifndef MATH_OPERATIONS_H
     #define MATH_OPERATIONS_H
    
     int add(int a, int b);
     int subtract(int a, int b);
    
     #endif // MATH_OPERATIONS_H
    
  3. math_operations.c: The implementation of the functions declared in math_operations.h.

     // math_operations.c
    
     #include "math_operations.h"
    
     int add(int a, int b) {
         return a + b;
     }
    
     int subtract(int a, int b) {
         return a - b;
     }
    

Now, let's break down how the #include directive works in this example:

  • main.c:

    • The #include <stdio.h> includes the standard input/output library, which provides functions like printf.

    • The #include "math_operations.h" includes the custom header file math_operations.h. This allows the main.c file to use the functions declared in math_operations.h without needing to know their implementations.

  • math_operations.h:

    • The #ifndef, #define, and #endif lines are used for header guards. They prevent the header file from being included multiple times in the same compilation unit, which helps avoid redefinition errors.

    • The file declares two functions add and subtract without providing their implementations. This is the interface that other files can use.

  • math_operations.c:

    • The #include "math_operations.h" line includes the same header file that was included in main.c. This ensures that the function declarations in math_operations.h match the actual implementations in this file.

    • The file provides the implementations for the add and subtract functions.

When you compile the program, you typically compile all source files together. For example:

gcc main.c math_operations.c -o my_program

This compilation command tells the compiler to compile both main.c and math_operations.c together, linking them into the executable my_program. The #include directives facilitate the organization of code into separate files and enable code reuse, making the program more modular and maintainable.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.