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.
Swap Two Numbers using Call By Reference in C Programming
int swap(int *ptr1, int *pt2);
int num1, num2;
printf("\nEnter The First Number:\t");
printf("\nEnter The Second Number:\t");
printf("\nThe Swapped Values are:\n");
printf("First Number = %d and Second Number = %d\n", num1, num2);
int swap(int *ptr1, int *ptr2)
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
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.