C program to swap two number by refrence.

Posted by Mangesh on April 26, 2018

/* program to swap two number by reference. */

Description :

In order to swap two number by using reference we need to change the refering address of the variables.

Program :

#include<stdio.h>

void swap(int * num1, int * num2)
{
    int temp;
    temp = *num1;
    *num1= *num2;
    *num2= temp;
}

int main()
{
    int num1, num2;
    printf("Enter first numbers : ");
    scanf("%d",&num1);
    printf("Enter second numbers : ");
    scanf("%d",&num2);
    printf("Before swapping");
    printf("Value of num1 = %d", num1);
    printf("Value of num2 = %d \n", num2);
    swap(&num1, &num2);
    printf("\nAfter swapping");
    printf("\nValue of num1 = %d", num1);
    printf("\nValue of num2 = %d", num2);
    return 0;
}

Output :

 c program to swap two number Executed and tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program to print address of variable.
2 C program to swap two number by refrence.
Latest Post
1 shubhanshu
2 test
3 C program for Breadth First Search.
4 C program for Depth First Search(DFS).
5 C for circular queue using linked list.