File Inclusion In C Programming Language


In today’s video tutorial lets learn more about include directive or preprocessor statement.

What We Learn?

1. Two ways of including/importing files.
2. Why write related code in separate file.
3. Example program to illustrate including and working with multiple files.

Related Read:
include directive in C Program

Video Tutorial: File Inclusion In C Programming Language


[youtube https://www.youtube.com/watch?v=E9zYMqf3qHk]

YouTube Link: https://www.youtube.com/watch?v=E9zYMqf3qHk [Watch the Video In Full Screen.]

Source Code: File Inclusion In C Programming Language

Main.c

#include<stdio.h>
#include "Circle.h"

int main()
{
    float r;

    printf("Enter radius of Circle\n");
    scanf("%f", &r);

    printf("Area of Circle is %f\n", circle_area(r, PI));

    return 0;
}

Output:
Enter radius of Circle
5
Area of Circle is 78.539819

In your Code::Blocks editor, go to File -> New -> Empty File.

Circle.c

double circle_area(float r, float PI)
{
    return( PI * r * r );
}

Circle.h

#define PI 3.14159265358979323846

double circle_area(float, float);

Note: Make sure Main.c, Circle.c and Circle.h are all in the same directory/folder.

Circle.c
This file has definition of the function/methods.

Circle.h
This file has all the macro definitions and function prototypes.

Main.c
This is our main C program where we include Circle.h file. By including this file we also include macro definition for PI and also the function to calculate area of circle.

Preprocessor includes all the content of Circle.h into Main.c source code before passing the code to the compiler.

Since all the code in Circle.h is included, we can directly use PI value as well as we can call circle_area() method from within Main.c file.

Advantages of Separating Related Files

1. If we’re using certain functions and macros repeatedly in our program, its best practice to write it in a separate file and include it at the top of our program and use the functions and macros wherever necessary.

Example: We keep using printf() and scanf() methods in all our programs, so it has been written in a separate library file called stdio.

2. In big projects, if we separate related files, we can handover related tasks to separate teams. This way we can maintain modularity of the project effectively.

Example: Math.h file has macro definition and prototype of all the functions related to Mathematics.

3. It’s easy to find bugs, debug and unit test our project.

4. Our source code looks less clumsy and more readable.

Two Ways To Include Files

There are 2 ways to include files in C.

1. Using angular brackets: Angular Brackets are used to import or include standard library files. In this case the linker looks for the included file in the standard library file storage directory / folder.

2. Using Double Quotes: When filename is wrapped with double quotes, the linker looks for the file inside current working directory. It also means that the included file is written by the user and is not a part of standard library file.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *