Reputation: 645
I tried to solve this problem in some test, but later when i ran it at home, it gave unexpected answer. I am not able to understand this code:
#include <stdio.h>
#include <conio.h>
#define swap(a,b) temp=a; a=b; b=temp;
int main()
{
int i, j, temp;
i=5;
j=10;
temp=0;
if( i > j) //evaluates to false
swap( i, j );
printf( "%d %d %d", i, j, temp); //expected output: 5 10 0
getch();
return 0;
}
Output i am getting is: 10 0 0
Please someone explain how is it working.
Upvotes: 1
Views: 248
Reputation: 30489
Code below
if( i > j) //evaluates to false
swap( i, j );
Becomes
if( i > j) //evaluates to false
temp=i; i=j; j=temp;
which is equivalent to
if( i > j) //evaluates to false
{temp=i;} i=j; j=temp;
If condition is false, there would be unexpected results as below
i=5;
j=10;
temp=0;
i=j; /* i becomes 10 */
j=temp; /* j becomes 0 */
Learnings
{}
#define swap(a,b) do { temp=a; a=b; b=temp; } while(0)
Note that there is no terminating semicolon after while(0)
Upvotes: 11
Reputation: 58271
Expanding the macro, you get:
if (i > j)
temp = i;
i = j;
j = temp;
This is why seasoned c-programmers wrap macro bodies in do{...}while(0)
.
#define swap(a, b) do{temp=a; a=b; b=temp;}while(0)
Upvotes: 3