C Program To Swap Two Numbers using Call By Reference
Learn How To Swap Two Numbers using Call By Reference in C Programming Language. Learn C Code To Swap Two Variables using Pointers with Addresses of their memory locations. Two variables can be swapped using Functions and without using Functions as well. This program to Swap Two numbers using Call By Reference method makes use of Pointers in C Programming.
The & Operator is known as a Reference Operator. It works as a Binary as well as a Unary Operator. However, the Unary & operator returns the Address of the Operand associated to it.
The * Operator or the Asterisk Operator is called as a De – Reference Operator. It is used with a Pointer Variable to return the Value of the variable.
Note: This C Program To Swap Two Variables using Call By Reference is developed with gEdit Editor and compiled using GCC Compiler in Linux Ubuntu Operating System.
Also Read: C Program To Swap Two Variables using Call By Value
Swap Two Numbers using Call By Reference in C Programming
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<stdio.h> int swap(int *ptr1, int *pt2); int main() { int num1, num2; printf("\nEnter The First Number:\t"); scanf("%d", &num1); printf("\nEnter The Second Number:\t"); scanf("%d", &num2); swap(&num1, &num2); printf("\nThe Swapped Values are:\n"); printf("First Number = %d and Second Number = %d\n", num1, num2); return 0; } int swap(int *ptr1, int *ptr2) { int temp; temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; } |
Also Read: C Program To Swap Two Variables using Temporary (Third) Variable
Output

Also Read: C Program To Swap Two Variables without using Temporary (Third) Variable
In case you get any Compilation Errors with this C Program To Swap Variables with Call By Reference method or if you have any doubt about it, mention it in the Comment Section.
flow chart please