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

What is stack in c?

0 Answers  


7. Identify the correct argument for the function call fflush() in ANSI C: A)stdout B)stdin C)stderr D)All the above

10 Answers   Accenture,


What is strcmp in c?

0 Answers  


How can you be sure that a program follows the ANSI C standard?

0 Answers   Aspire, Infogain,


Please write me a program to print the first 50 prime numbers (NOT between the range 1 -50)

5 Answers   IBM, KJH,






Write a program to print numbers from 1 to 100 without using loop in c?

0 Answers  


int x=sizeof(!5.856); What will value of variable x?

2 Answers  


Can you explain the four storage classes in C?

0 Answers   TCS,


What is the memory allocated by the following definition ? int (*x)();

2 Answers   ADITI,


How pointer is different from array?

0 Answers  


Why doesnt this code work?

0 Answers  


What is the purpose of void pointer?

0 Answers  


Categories