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

Macro With Argument v/s Function: C Program

In today’s video tutorial lets see the difference between a Macro with argument and a simple function.

Related Read:
Macros With Arguments: C Program
Function / Methods In C Programming Language

Video Tutorial: Macro With Argument v/s Function Call: C Program


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

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

Source Code: Macro With Argument v/s Function Call: C Program

#include<stdio.h>

#define PI 3.14159265358979323846
#define AREA(r) ( PI * r * r )

double circle_area(float);

int main()
{
    float r;

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

    printf("Area of Circle, using Macro is %f\n", AREA(r));
    printf("Area of Circle, using Function is %f\n", circle_area(r));

    return 0;
}

double circle_area(float r)
{
    return( 3.14159265358979323846 * r * r );
}

Output:
Enter radius of Circle
5
Area of Circle, using Macro is 78.539816
Area of Circle, using Function is 78.539816

As you can see in above output the result is same for Macro and function call. Now the question is, when to use macros and when to use functions?

When to use macros and when to use functions?

Macro: If a macro template is used 100 times inside a C program, all these macro template will be replaced with its macro expansion before its passed to the compiler. This process is called as preprocessing, and it’s done by preprocessor. Because of this, the size of the program increases.

Function: Function definition is written only once and even if its called 100 times inside a program it won’t increase the program size. Whenever there is a function call, the control is passed to the function definition where some logical operation is performed and then a meaningful result is returned back to the calling function. This takes some time.

So the trade off is between memory space and time.

If memory is not an issue, then you can simply use Macros – as it brings in speed of execution as an advantage. But if memory space is an issue – if you’re writing for a mobile device, then it’s better to go with functions. Though its slower, it uses less memory space.

While coding for bigger real-time projects you’ll surely need the knowledge of macros and macros with argument, so know the syntax and it’s advantages. In this fast moving world speed of execution really matters. And C programming specifically is famous and used often for its speed and its easy integration(and interaction) with the hardware devices.

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

Using Macro Template In Macro Expansion: C Program

In this video tutorial lets see how we can make use of a macro template in a macro expansion.

Objective

We define PI in a macro, and then we use PI in expansion of another macro.

We shall also modify the same program to make use of constant M_PI present in math.h library file instead of macro template PI.

Related Read:
Preprocessors In C Programming Language
C Program To Find Area of Circle Using Macro Expansion
Macros With Arguments: C Program

Video Tutorial: Using Macro Template In Macro Expansion: C Program


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

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

Source Code: Using Macro Template In Macro Expansion: C Program

#include<stdio.h>

#define PI 3.14159265358979323846

#define AREA(r) ( PI * r * r )

int main()
{
    float r;

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

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

    return 0;
}

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

In above source code, preprocessor replaces every occurrence of PI with 3.14159265358979323846 and then checks for every occurrence of AREA(r) and replaces with ( 3.14159265358979323846 * r * r ).

With this simple example I’m trying to illustrate that we can make use of Macro template inside macro expansion of another macro definition.

Using M_PI constant present in Math.h header file

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

#define AREA(r) ( M_PI * r * r )

int main()
{
    float r;

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

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

    return 0;
}

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

Note: M_PI is also a macro inside math.h 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

Macros With Arguments: C Program

In this video tutorial lets see how to write macros with arguments. We’ll illustrate finding biggest of 2 numbers by using “Macros with arguments” concept.

Note: Do not leave space between macro template name and the argument list / parenthesis. Also make sure to write the macro expansion inside parenthesis.

Related Read:
Preprocessors In C Programming Language
C Program To Find Area of Circle Using Macro Expansion

Video Tutorial: Macros With Arguments: C Program (Biggest of 2 Numbers)


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

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

Source Code: Macros With Arguments: C Program

#include<stdio.h>

#define MAX(x, y) ( x > y ? x : y )

int main()
{
    int a, b;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    printf("Biggest of %d and %d is %d\n", a, b, MAX(a, b));

    return 0;
}

Output 1:
Enter 2 numbers
14
5
Biggest of 14 and 5 is 14

Output 2:
Enter 2 numbers
5
14
Biggest of 5 and 14 is 14

Functions return result after evaluating the expression, but Macros are different. Before compiler compiles the program, preprocessor replaces all the occurrence of Macro template with Macro expansion. So in macros, it won’t return the result, but instead the entire macro expansion gets placed in place of macro template.

So in above C program, printf() becomes:

printf("Biggest of %d and %d is %d\n", a, b, ( a > b ? a : b ));

Related Read:
Biggest of Two Numbers Using Ternary Operator: C

Check Below 2 Source code and it’s output

No Parenthesis around macro expansion

#include<stdio.h>

#define SQUARE(n) n * n

int main()
{
  printf("%f", 64/SQUARE(4));

  return 0;
}

Output:
64

Parenthesis around macro expansion

#include<stdio.h>

#define SQUARE(n) (n * n)

int main()
{
  printf("%f", 64/SQUARE(4));

  return 0;
}

Output:
4

In C programming and in mathematics in general, each arithmetic operation has different precedence. Hence by using parenthesis around macro expansion, it gets the highest precedence.

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

C Program To Find Area of Circle Using Macro Expansion

In this video tutorial lets learn about Macro Expansion and also lets write a simple program to find area of Circle using Macros.

Related Read:
Preprocessors In C Programming Language

Rules To Construct/Write Macro Definition

1. Space between # and define is optional.
2. There must be a space or tab between Macro Template and Macro Expansion.
3. #define MACRO_TEMPLATE MACRO_EXPANSION is called Macro Definition.
4. Macro definition must not end with semi-colon.
5. Using all capital letters for Macro template is convention and not a syntax of C. You can use any name for Macro template, except the C reserve words.

Video Tutorial: C Program To Find Area of Circle Using Macro Expansion


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

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

Source Code: C Program To Find Area of Circle Using Macro Expansion

#include<stdio.h>

#define PI 3.14

int main()
{
    float r, area;

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

    area = PI * r * r;

    printf("Area of Circle is %f\n", area);

    return 0;
}

Output 1:
Enter Radius of Circle
5
Area of Circle is 78.500000

Source Code: Writing printf statement in Macro expansion

#include<stdio.h>

#define DISPLAY printf("I'm in Love with iPhone UI Elements\n\n")

int main()
{
    DISPLAY;
    return 0;
}

Output 1:
I’m in Love with iPhone UI Elements

Note: Careful not to enter semicolon at the end of Macro definition. i.e., after the end of printf() statement.

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