Coupon Accepted Successfully!


Question 1

How should we write a multi-statement macro?

This is the correct way to write a multi statement macro.

#define MYMACRO(argument1, argument2) do { \
                                 stament1; \
                                 stament2; \
                                } while(1) /* no trailing semicolon */

Question 2

How can I write a macro which takes a variable number of arguments?

One can define the macro with a single, parenthesized "argument" which in the macro expansion becomes the entire argument list, parentheses and all, for a function such as printf():

#define DEBUG(args) (printf("DEBUG: "), printf args)

if(n != 0) DEBUG(("n is %d\n", n));

The caller must always remember to use the extra parentheses. Other possible solutions are to use different macros depending on the number of arguments. C9X will introduce formal support for function-like macros with variable-length argument lists. The notation ... will appear at the end of the macro "prototype" (just as it does for varargs functions).

Question 3

What is the token pasting operator and stringizing operator in C?

Token pasting operator

ANSI has introduced a well-defined token-pasting operator, ##, which can be used like this:

#define f(g,g2) g##g2

  int var12=100;



Stringizing operator

#define sum(xy) printf(#xy " = %f\n",xy);

    sum(a+b); // As good as printf("a+b = %f\n", a+b);

So what does the message "warning: macro replacement within a string literal" mean?

#define TRACE(var, fmt) printf("TRACE: var = fmt\n", var)
TRACE(i, %d);

gets expanded as

printf("TRACE: i = %d\n", i);

In other words, macro parameters were expanded even inside string literals and character constants. Macro expansion is *not* defined in this way by K&R or by Standard C. When you do want to turn macro arguments into
strings, you can use the new # preprocessing operator, along with string literal concatenation:

#define TRACE(var, fmt) printf("TRACE: " #var " = " #fmt "\n", var)

See and try to understand this special application of the strigizing operator!

#define Str(x) #x
#define Xstr(x) Str(x)
#define OP plus

char *opname = Xstr(OP); //This code sets opname to "plus" rather than "OP".

Here are some more examples


Define a macro DEBUG such that the following program

int main()
  int x=4;
  float a = 3.14;
  char ch = 'A';

  DEBUG(x, %d);
  DEBUG(a, %f);
  DEBUG(ch, %c);


DEBUG: x=4
DEBUG: y=3.140000

The macro would be 

#define DEBUG(var, fmt) printf("DEBUG:" #var "=" #fmt "\n", var);


Write a macro PRINT for the following program

int main()
 int x=4, y=4, z=5; 
 int a=1, b=2, c=3;

such that it outputs

x=4 y=4 z=5
a=1 b=2 c=3

Here is a macro that will do this

#define PRINT(v1,v2,v3) printf("\n" #v1 "=%d" #v2 "=%d" #v3 "=%d", v1, v2, v3);

Question 4

Define a macro called SQR which squares a number.

This is the wrong way of doing it.

#define SQR(x) x*x

Thats because, something like

#include stdio.h>
#define SQR(x) x*x

int main()
  int i;
  i = 64/SQR(4);

will produce an output of 64, because of how macros work (The macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64).

This is the right way of doing it.

#define SQR(x) ((x)*(x))

But, if x is an expression with side effects, the macro evaluates them twice. The following is one way to do it while evaluating x only once.

#define SQR(x) pow((x),2.0)

Test Your Skills Now!
Take a Quiz now
Reviewer Name