I don't understand how to debug the following code "on paper" (without a compiler):

#include <stdio.h>
#define SUB(T,x,y) {T x=y++; {T x=y++;}}
int x;
static int y;
void f(int n)
{ if (n) { SUB(int,x,y) f(n-1); } }
extern int x,y;
int i=0;
for ( ; ++i<3; )
f(i); printf("%d ",y);
return (0);

How to evaluate values of a macro SUB?

1 Year
Discussion Span
Last Post by negru

It is saying basicially that the first argument is a type for x and y. Then it assigns to x the value of y, and then increments y. Next it creates a new variable x and assigns the current value of y to that, as well as incrementing the value of y. At the end of all of this, y == the original value of y + 2, but since nothing is done with that, it is thrown away. Likewise, since x is not returned, it too is thrown away. Result? Nothing, although since you define y as an extern variable, it will retain the final value, including after the function f(int n) is called. The function f(int n) is a recursive function. If the value of n is not 0, then it will execute the macro SUB() and thereby increase the value of y by two.

Confused yet? :-)


@rubberman From where the value of n comes from? Also, variable i is initialized to zero, but has a value 1 in main(). What are the values of SUB(int,x,y) f(n-1)?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.