Roshan
Roshan

Reputation: 645

Explanation needed for this pre-processor directive C/C++

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

Answers (2)

Mohit Jain
Mohit Jain

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

  1. Try to put blocks (if, else, for, do, while) inside {}
  2. Avoid macros, prefer functions
  3. If you must use macros, use safe macros, for ex:

#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

Paul Hankin
Paul Hankin

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

Related Questions