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
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 |
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 |
What is class and object in c?
Explain modulus operator. What are the restrictions of a modulus operator?
What is memcpy() function?
What does s c mean on snapchat?
how to make program without <> in library.
What are two dimensional arrays alternatively called as?
what type of language is C?
what is the difference between c and c++?
what is the output for this question: main() { int i=1; printf("%d%d%d",i,i++,++i); }
Can we use any name in place of argv and argc as command line arguments?
In C program, at end of the program we will give as "return 0" and "return 1", what they indicate? Is it mandatory to specify them?
What's the best way of making my program efficient?