What will be printed as the result of the operation below:

#define swap(a,b)  a=a+b;b=a-b;a=a-b;
void main()
{
int x=5,y=10;
swap(x,y);
printf("%d%d",x,y);
}

Recommended Answers

All 3 Replies

It's fairly simple to just compile that and find out yourself. If you don't have a compiler you can translate the macro yourself to get the following source:

void main() {
   int x=5,y=10;
   x=x+y;y=x-y;x=x-y;;
   printf("%d%d",x,y);
}

From that point it's just straight addition and subtraction. You should be able to compute that easy enough.

According my point of view if you want to know the output of this code then it is swaping process
so look below
x=5,y=10;
x=x+y;//x=5+10 now x value is 15
y=x-y;//y=15-10 now y value is 5
x=x-y;//x=15-5 now x value is 10
so it's simple swaping process guys
At the end you will be find value as
x=10 and y=5;

Thanks ..That's all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.