How to swap two variables, without using third variable ?
Answers were Sorted based on User's Feedback
Answer / marutikutre
x = y | (y=x)-x;
&
a=a*b/(b=a); provided either of a or b is not 0
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / mrx
In assembly language you could use "xchg" instruction witch
swaps the two variables in one instruction.
/* swaps %dest <-> %source */
xchg %dest, %source
/* inline assambly example for C language */
void swap(int register *x, register int *y)
{
asm("xchg %0, %1;" \
: "=r"(*x), "=r"(*y)
: "0"(*x), "1"(*y)
);
}
This is the fastest method you can achieve in hand
optimization. I think C compiler will do the same trick if
possible.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / simhachalam lakkakula
Answer 4 get fail for the following values
Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / simhachalam lakkakula
Answer 4
Logic:
a=a*b;
b=a/b;
a=a/b;
get fail for the following values
Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1
Is This Answer Correct ? | 1 Yes | 0 No |
---------------------
a=a*b
b=a/b
a=a/b
---------------------
a=a+b
b=a-b
a=a-b
----------------------
a = a Xor b
b= a Xor b
a= a Xor b
----------------------
all above three methods are correct, i hv checked...
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / rahul
I am new in C/C++.
How to write the program for second answer. Does it
automatically convert the numbers in binary by using ^ sign.
:}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / devarajan.k
a[12]=name
b[20]=name
Here the size 12 & 25 has no effect hence we can change
the size
a[]=name
b[]=name
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / poojamnagal
Num1 = 5
Num2 = 7
Num1 = 5+7 = 12
Num2 = 12- 7 = 5
Num1 = 12-5 = 7
So, Now Num1 = 7
Num2 = 5
Is This Answer Correct ? | 2 Yes | 2 No |
main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }
What are segment and offset addresses?
why is printf("%d %d %d",i++,--i,i--);
write a program to count the number the same (letter/character foreg: 's') in a given sentence.
main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); }
#include<stdio.h> main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); }
#include<stdio.h> #include<conio.h> void main() { int a=(1,2,3,(1,2,3,4); switch(a) { printf("ans:"); case 1: printf("1");break; case 2: printf("2");break; case 3: printf("1");break; case 4: printf("4");break; printf("end"); } getch(); }
#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }
C program to print magic square of order n where n > 3 and n is odd
main() { char *p="GOOD"; char a[ ]="GOOD"; printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p), strlen(p)); printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a)); }
hello sir,is there any function in C that can calculate number of digits in an int type variable,suppose:int a=123; 3 digits in a.what ll b answer?
How we print the table of 3 using for loop in c programing?