Categories
Preprocessor and Macros

Macros using #define

A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive.

Here’s an example.

#define c 299792458  // speed of light

Here, when we use c in our program, it is replaced with 299792458.

Example 1: #define preprocessor

#include <stdio.h>
#define PI 3.1415

int main()
{
    float radius, area;
    printf("Enter the radius: ");
    scanf("%f", &radius);

    // Notice, the use of PI
    area = PI*radius*radius;

    printf("Area=%.2f",area);
    return 0;
}

Leave a Reply

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