Using Call by Reference Swap Numbers – Write a C program to swap two values by call by reference method of parameter passing.
Program for Passing Parameters – Call By Reference
#include <stdio.h> void swap2numbers(int *n1, int *n2); int main() { int num1,num2; printf("\n\n Using C Pointers : Swap Numbers Using Call by Reference :\n"); printf("--------------------------------------------------------------\n"); printf(" Enter First Number : "); scanf("%d",&num1); printf(" Enter Second Number : "); scanf("%d",&num2); printf("\n The values before swapping are :\n"); printf(" Number 1 = %d\n Number 2 = %d\n",num1,num2); swap2numbers(&num1, &num2); printf("\n The values after swapping are :\n"); printf(" Number 1 = %d\n Number 2 = %d\n",num1,num2); return 0; } void swap2numbers(int *n1, int *n2) { int temp; temp=*n1; *n1=*n2; *n2=temp; }
Output
Using C Pointers : Swap Numbers Using Call by Reference :
————————————————————–
Enter First Number : 105
Enter Second Number : 600
The values before swapping are :
Number 1 = 105
Number 2 = 600
The values after swapping are :
Number 1 = 600
Number 2 = 105
You will also like to learn more:
- Using Pointers Count Vowels
- Using Pointers Find String Length
- Python GCD Recursive Function
- Python Power Recursive Function
- Python Recursive Function Add Numbers
- Average of Array With Pointer
- Sum of Array Using Pointers
- Using Pointers With Array Input Output
- Understanding C Pointers
- Using Pointers Add Numbers