Wednesday, March 24, 2010

70

#define les(a,b)     a<b?a:b
int main()
{
int i=1,j=3,k;
k=les(i++,++j);
printf("%d %d %d",i,j,k);
return 0;
}

Output:

3  4   2


Logic:
While compilation happens ,macro identifier is replaced with associated character sequence.
i.e.,
les(i++,++j) is replaced by i++<++j?i++:++j
so now

k=i++<++j?i++:++j;
1 4 2


In this statement 1<4,so i++ is assigned to k(statement is true so 1st variable is returned i.e,i++).
k=i++;
2

so k=2 and 'i' is increment later(post increment) .so 'i' will became 3.

0 comments:

Post a Comment