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 the benefit of using const for declaring constants?

0 Answers  


What are examples of structures?

0 Answers  


What is sorting in c plus plus?

0 Answers  


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

5 Answers   Appin, IBM,


write a program to find the sum of the array elements in c language?

24 Answers   ICT, Infosys, Wipro,






We can draw a box in cprogram by using only one printf();& without using graphic.h header file?

4 Answers   NIIT,


What is selection sort in c?

0 Answers  


a memory of 20 bytes is allocated to a string declared as char *s then the following two statements are executed: s="Etrance" l=strlen(s); what is the value of l ? a.20 b.8 c.9 d.21

4 Answers   TCS,


What is meant by high-order and low-order bytes?

0 Answers  


What is a rvalue?

0 Answers   Global Logic,


How do I determine whether a character is numeric, alphabetic, and so on?

0 Answers  


Which one would you prefer - a macro or a function?

0 Answers  


Categories