Can u write a C program to swap two integers that does not use a temporary variable (or that uses only two variables to be swapped)?
Here goes an implementation:
#include "stdio.h"
int main()
{
int a=1,b=3;
printf("BEFORE SWAPPING : a=%d & b=%d\n",a,b);
a^=b^=a^=b;
printf("AFTER SWAPPING : a=%d & b=%d\n",a,b);
}
The output looks something like this:
BEFORE SWAPPING : a=1 & b = 3
AFTER SWAPPING : a=3 & b = 1
Looks good isn't it?
A Collection Of Weird and Interesting C Programs by Balaji
Monday, August 20, 2007
Interesting C Program -16
Posted by
Balaji V
at
10:42 AM
Labels: INTERESTING C PROGRAMS
Subscribe to:
Post Comments (Atom)







0 comments:
Post a Comment