can u write a program in C, which does not use = (eqaul)or
any arithmatic assignment(like -=,+=,*= etc) operator to
swap to number?

Answers were Sorted based on User's Feedback



can u write a program in C, which does not use = (eqaul)or any arithmatic assignment(like -=,+=,*..

Answer / musa

swapping x and y using z as intermediary

memcpy(&z, &x, sizeof(x));
memcpy(&x, &y, sizeof(x));
memcpy(&y, &z, sizeof(x));

Is This Answer Correct ?    2 Yes 1 No

can u write a program in C, which does not use = (eqaul)or any arithmatic assignment(like -=,+=,*..

Answer / anish

Simple... use XOR (^) operator...
example suppose,
x=101 (binary)
y=010 (binary)
now,
Execute : x=x^y; -->> from LSB 1^0=1 0^1=1 1^0=1
now x=111
Execute : y=x^y; -->> from LSB 1^0=1 1^1=0 1^0=1
now y=101 (101 used to be the value of x)
Execute : x=x^y; -->> from LSB 1^1=0 1^0=1 1^1=0
now x=010 (010 used to be the values of y)

code snippet
{
x=10;
y=30;
//swap x and y
x=x^y;
y=x^y;
x=x^y;
}

now x=30 and y=10

any doubts please ask me....

Is This Answer Correct ?    3 Yes 8 No

Post New Answer

More C Interview Questions

FORMATTED INPUT/OUTPUT functions are a) scanf() and printf() b) gets() and puts() c) getchar() and putchar() d) all the above

0 Answers  


Wt are the Buses in C Language

0 Answers   Infosys,


How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

2 Answers   CMC, Wipro,


what is the stackpointer

2 Answers  


Write a program to show the workingof auto variable.

2 Answers   Infotech,






What are header files and what are its uses in C programming?

0 Answers  


What does it mean when the linker says that _end is undefined?

0 Answers  


Can we access array using pointer in c language?

0 Answers  


How to set a variable in the environment list?

1 Answers  


c program to input values in a table(using 2D array) and print odd numbers from them

1 Answers  


What is 1f in c?

0 Answers  


Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database

2 Answers   TCS, Unisys, Webyog,


Categories