Program to Swap Two Numbers using Call By Reference in C

Source Code

// 43. Program to swap two numbers using call by reference
// Arpan Das - CST 2nd Year (VOCLET)

#include <stdio.h>
//#include <conio.h>

void main () {
	int x, y;
	void swap(int *, int *);
	//clrscr();

	printf("\t\t..:: swap two numbers using call by reference ::..\n\n");
	printf("Please enter values for variables X and Y respectively: ");
	scanf("%d%d", &x, &y);

	printf("\nNow X = %d and Y = %d", x, y);
	swap(&x, &y);
	printf("\nSwapped...\nNow X = %d and Y = %d", x, y);

	//getch();
}

void swap (int *x, int *y) {
	int z = *x;

	*x = *y;
	*y = z;

	//*x = (*x+*y)-(*y=*x);
}

Output

	..:: swap two numbers using call by reference ::..

Please enter values for variables X and Y respectively: 4 8

Now X = 4 and Y = 8
Swapped...
Now X = 8 and Y = 4

Leave a Reply

Note: Your email address will not be published. Required fields are marked *