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

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

Calculate Area of a Circle using math.h library: C

We can calculate area of circle if we know the value of its radius. So using that, we shall write the logic in c program to calculate the area of a circle, and we will us math.h library functions in this program.

Related Read:
Basic Arithmetic Operations In C

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

int main()
{
    float radius;

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

    printf("Area of circle is %f \n", (M_PI * pow(radius, 2)));

    return 0;
}

Output:
Enter radius of circle
2.0
Area of circle is 12.566371

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Calculate Area of a Circle using math.h library: C


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

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


math.h library file has a constant M_PI which has the value of PI. Since it’s a constant variable we can not change its value in our program. We’re also using pow() method/function present in math.h header file.

Formula for calculating Area of a circle


area of circle

Formula for calculating Area of a circle is PI * radius * radius. We take value of radius from user and using above formula calculate area of the circle and output result to the console window. Since we’re using math.h library file we use M_PI and pow() methods for the expression.

i.e., area = M_PI * pow(radius, 2);

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

Calculate Area of a Circle without using math.h library: C

We can calculate area of circle if we know the value of its radius. So using that, we shall write the logic in c program to calculate the area of a circle, without using math.h library functions.

Related Read:
Basic Arithmetic Operations In C

 
#include < stdio.h >

int main()
{
    float radius;
    const float PI = 3.14;

    printf("Enter the value for radius \n");
    scanf("%f", &radius);

    printf("Area of the circle is %f\n", (PI * radius * radius));

    return 0;
}

Output:
Enter the value for radius
5.0
Area of the circle is 78.500003

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Calculate Area of a Circle without using math.h library: C


[youtube https://www.youtube.com/watch?v=9-_G0N4HnLc]

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


Whenever we declare and initialize a constant, its a convention to make use of capital letter variable. That way we can easily spot and identify a constant in our program. And as we already know, value of constant can not be changed through out the program execution.
Ex: const float PI = 3.14;

Formula for calculating Area of a circle


area of circle

Formula for calculating Area of a circle is PI * radius * radius. We take value of radius from user and using above formula calculate area of the circle and output result to the console window.

Note that we’ve not used math.h library and any of its methods/functions in our program.

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