/* EXCHANGE THE INTEGER VALUE WITHOUT USING THE THIRD VARIABLE */
    #include<stdio.h>
    void main()
    {
     int a,b;
      printf("enter the two integer variabe \n");
      scanf("%d,%d",&a,&b);
      //exchange the value of a to b ,b to a without using third variable 
      a=a+b;
      b=a-b;
      a=a-b;
      printf("%d%d",a,b);
      getch();
     }

/*EXAMPLE: A=3,B=10
  A=A+B;  // A=3+10=13
  B=A-B;  //B=3-10=3
  A=A-B   //A=13-3=10 */

Recommended Answers

All 12 Replies

Yeah, so? Why would anyone want to do this? It's hard to understand and using a temp variable is clean and understandable.

yes, I compelet agree with u in normal time,but when it comes to memory storage or allocation its better to go in this way,time efficience compare to using the third or temp variable

but when it comes to memory storage or allocation its better to go in this way,time efficience compare to using the third or temp variable

Really? Please show us why 4 extra bytes for a temp variable is slower than all that math. What are your sources for this assumption of speed vs memory useage? Document your position.

but when it comes to memory storage or allocation its better to go in this way

Memory footprint is a viable argument, but I'd suggest that if the memory for a variable of type int is too much, you have bigger problems. I'm not sure what you mean by allocation though, as local variables are "allocated" with a simple adjustment of the stack pointer.

time efficience compare to using the third or temp variable

Using a temporary variable is all but guaranteed to be more efficient. This is a case of premature pessimization, where the "clever" optimization trick actually makes the code perform worse than the naive code would by confusing the compiler's optimization routines.

#include <stdio.h>
#include <stdlib.h>
void main()
{
    int a,b;
    printf("enter two numbers: ");
    scanf("%d%d",&a,&b);
    printf("before swapping....\n");
    printf("A=%d B=%d",a,b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("after swapping...\n");
    printf("A=%d B=%d",a,b);
}

here try this man ull get perfect output

ur trying to swap right???

commented: No, he's exchanging. Read the thread AND the code he posted. -3

Here is another Logic for the same :

#include<stdio.h>
#include<stdlib.h>
void main()
{
    int a,b;
    printf("\nEnter two numbers: ");
    scanf("%d %d",&a,&b);
    printf("\nBefore swapping :");
    printf("\nA=%d \t B=%d",a,b);
    a=a*b;
    b=a/b;
    a=a/b;
    printf("\nAfter swapping :");
    printf("\nA=%d \t B=%d",a,b);
}

I'm shocked nobody has pulled out the XOR trick yet. That's usually the first one that pops up when people try to show off the trivia they've learned that has no practical benefit in the real world.

When developing in low-level languages, particularly assembly languages, and when a low number of registers or limited memory is available, the use of a temporary location could be restrictive

When developing in low-level languages, particularly assembly languages

You'll notice that this is the C forum, not the assembly forum.

when a low number of registers or limited memory is available

As I mentioned already, that can be a viable argument in favor of the tricks. But that's a niche area, and those platforms are the ones that don't support stdio.h (which you used in your OP). Further, when it is a viable argument, the caveats are made very clear to discourage use outside of the niche area, and you didn't do that. Rather, you tried to palm this off as a generally applicable and appropriate solution, which it isn't.

just i shared the idea for swapping value with out using the three variable
many of know this better then me
this is for who is search swapping without three variable

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.