Macro with argument - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Sunday 5 November 2017

Macro with argument

#define can be used to write macro definitions with parameters.
Syntax :
#define identifier(identifier 1,.....identifier n) token_string

Token string: is optional but used in every case.
Example :

    #include <stdio.h>
    #define PI 3.1415
    #define area(r) (PI*(r)*(r))
    int main()
    {
    int radius;
    float area;
    printf("Enter the radius : ");
    scanf("%d",&radius);

    area=area(radius);
    printf("\nArea=%.2f",area);
    return 0;
    }

Output :
Enter the radius :
Area=12.57


Explanation :
Every time the program encounters area(radius), it will be replace by (3.1415*(r)*(r)).

No comments:

Post a Comment

Post Top Ad