Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

C program to swap two numbers using pointer

C program to swap two numbers using pointer

In this tutorial we will going to learn how to swap two numbers using pointer. If you want to learn more about pointer then you can go through my previous post Pointers in C Programming with Examples.

My System Configuration :

Fujitsu A555 with 12GB RAM, OS – Kubuntu 18.04, IDE used Geany (you can use your own favorite like Notepad++, Codeblock, Kate, Gedit, Sublime text …,  Compiler : GCC .

Complete Code for C program to swap two numbers using pointer

* Filename : swappointer.c
* C program to swap two no
* using pointer
* By Sanjay Prasad
#include <stdio.h>
void swap(int *n1, int *n2){
int temp;
int main(){
int a,b;
printf("Enter 1st no. a : ");
printf("\nEnter 2nd no. b: ");
printf("\na contain %d",a);
printf("\nb contain %d",b);
printf("\nAfter Swap \na contain %d",a);
printf("\nAfter Swap \nb contain %d",b);
return 0;

How to Compile and Run :

first create a file named swappointer.c somewhere. Open terminal and using cd to go there and use command given below :

gcc swappointer.c  (will compile code)

./a.out   (will execute code)


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Do you have any Query / Suggestion