DIGIT-ARC read-only archive · snapshot: July 23, 2026

Please Solve this C program

2 posts · originally from https://geek.digit.in/community/threads/please-solve-this-c-program.79366/

debiprasad_sahoo
debiprasad_sahoo Original post #1
Jan 30, 2008 at 11:38 PM
int main()
static int a=5,b=6,temp;
#define swap(a,b)temp=a;a=b;b=temp;
{
if(a>b)
swap(a,b)
printf("a=%d b=%d",a,b);
return(0);
}

The output is:
a=6 b=0
How & why?
K
Jan 31, 2008 at 10:45 PM
There are two solution i can give.

1.Solution:

static int a=5,b=6,temp;
#define swap(a,b)temp=a;a=b;b=temp;
int main()
{
if(a>b)
{
swap(a,b)
}
printf("a=%d b=%d",a,b);
return(0);
}

The o/p is a=5 ,b=6

2. Solution:

static int a=5,b=6,temp;
#define swap(a,b) { temp=a;a=b;b=temp; }
int main()
{
if(a>b)
swap(a,b)
printf("a=%d b=%d",a,b);
return(0);
}

The o/p is a=5 ,b=6

The problem was the #define instruction (preprossor) didn't close well...

Try this:

static int a=5,b=6,temp=99;
#define swap(a,b)temp=a;a=b;b=temp;
int main()
{
if(a>b)
swap(a,b)

printf("a=%d b=%d",a,b);
return(0);
}

The o/p is a=6 ,b=100