Macros

Macros :

A macro is a fragment of code which has been given a name and whenever, this name is used, it is replaced by that code itself. There are two kinds of macros. Object-like macro resemble data objects when used, function-like macro resemble function calls.

Object-like macros:

In this type of macro, a simple identifier is replaced by a code. They are called object because they look like an object when used in program. It is basically used to give constants some name.

e.g.

#define pi 3.14

It defined a macro pi. Wherever a pi is used in a program, it would be replaced by a 3.14 constant value (done by complier itself).

Function like macros:

Macro can also be declared which can look like a function.

e.g

c=ADD(3,4);

is replaced by compiler as

c=3+4;

PRINT();

This is replaced in code as:

printf(“Hello”);

and prints hello everytime it is called.

Stingizing token:

Converts token to string.# operator is used

e.g.

#define ADD(x,y) {x+y;printf(#x+y”equals%d”,x+y);}

Pasting token :

It is used to paste a value of token. ## is used.

e.g.

#define paste(a) printf(“value of”#a”is %d”,##a);

Predefined macros :

    • _TIME_:Gives current time as HH:MM:SS as character literal.
    • _DATE_:Gives current date as MMM DD YYYY as character literal
    • _FILE_:Gives current file name as string literal
    • _STDC_:Gives 1 if compiler uses ANSI standard
    • _LINE_:Gives current line as decimel literal

Leave a Reply

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