Thursday, 16 February 2012

Swap two variable value using C programming without using third variable or temporary variable


/* Without using any third varible*/

#include<stdio.h>

  void SwapInCv(int x , int y) ;  //function reference

  void main()
     {
             int a,b ;
             a=1 ;
             b=2 ;
         
           SwapInC(a,b) ;  //swap function call
  
             printf("values after swaping a=%d & b=%d ", a , b) ;
     }  //End of main

   void SwapInC(int x,int y)  //definition of Swap functon
    {
       y= x-y+(x=y);
    }

Common Programming Error:-
C compiler read from left to right to do not confound with swap or switch logic.

Good Programming Practice:-
always use equation like  i used instead using another temporary storage.

No comments:

Post a Comment

Compiler for C,C++ and Python {paste your programme to see the output}