C Program to Swap Two Numbers Using Pointers

For me pointers are the only reason I love C Programming. It broadens the concept of programming. And as a programmer I use to do programming through pointers mainly, as it eliminates many limitations of the program smartly. We can’t achieve swapping of two numbers without pointers when we are using a function call because every time actual arguments gets copied and being pass to the function, and becomes the formal arguments of that function. Any change in formal argument won’t make the actual arguments change. So cleverly we can pass the addresses as the formal arguments of the variables and can change the values using addresses. 

Look at the Program Carefully ►►



void swap(int *,int *);            //Function Prototype Declaration

void main()


int a,b;

printf(“Enter the values of A and B\n”);

scanf(“%d %d”,&a,&b);

swap(&a,&b);                          //Function Call

printf(“After Swapping … \n a=%d \t b=%d”,a,b);



void swap(int *x,int *y)        //Function Definition


int temp;





What happen is, we simply pass the addresses of variables(a,b) through ‘address of operator’ (&,ampersand) and receive it in the function using pointers. Now we know the addresses are the place where the values actually stored so we simply inter-change the values on that addresses. And addresses are the unique for every variable so wherever in the program we done something on the addresses, we can see the change anywhere in the program.


One thought on “C Program to Swap Two Numbers Using Pointers”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s