C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Output

Enter the radius: 3

Area=28.27

Syntactic Sugar

Syntactic sugar is the alteration of programming syntax according to the will of programmer. For example:

#define LT <

Every time the program encounters LT, it will be replaced by <.

Second form of preprocessing directive with #define is:

Macros with argument

Preprocessing directive #define can be used to write macro definitions with parameters as well in the form below:

#define identifier(identifier 1,.....identifier n) token_string

Again, the token string is optional but, are used in almost every case. Let us consider an example of macro definition with argument.

#define area(r) (3.1415*r*r)

Here, the argument passed is r. Every time the program encounters area(argument), it will be replace

by(3.1415*argument*argument). Suppose, we passed (r1+5) as argument then, it expands as below:

area(r1+5) expands to (3.1415*(r1+5)(r1+5))

C Program to find area of a circle, passing arguments to macros. [Area of circle=πr2]

#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("Area=%.2f",area);

return 0;

}

Predefined Macros in C language