Please Solve this C program

Status
Not open for further replies.

debiprasad_sahoo

Web Junky 2.0
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?
 

kpmsivachand

SivaChand
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
 
Status
Not open for further replies.
Top Bottom