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

How can my program discover the complete pathname to the executable from which it was invoked?

0 Answers  


Explain how can I manipulate strings of multibyte characters?

0 Answers  


what is pointer ?

10 Answers   Kernex Micro Systems,


main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf("%d %d\n",x,y); } what is the output?

10 Answers   Ramco,


C passes By value or By reference?

5 Answers   Geometric Software, Infosys,






How do shell structures work?

0 Answers  


i = 25;switch (i) {case 25: printf("The value is 25 ");case 30: printf("The value is 30 "); When the above statements are executed the output will be : a) The value is 25 b) The value is 30 c) The value is 25 The value is 30 d) none

0 Answers  


find the sum of two matrices and WAP for it.

0 Answers   Huawei,


Explain pointers in c programming?

0 Answers  


A collection of functions,calls,subroutines or other data a) library b) header files c) set of files d) textfiles

0 Answers  


How to add two numbers without using semicolon at runtime

2 Answers  


write a program to insert an element at the specified position in the given array in c language

5 Answers   Appin, IBM,


Categories