C Programming

Swapping

Learn C programming for swapping numbers using call by reference

6/12/2018
0 views
swapping.cC
#include<stdio.h>

/* Function 'swap' with pointer parameter */
void swap(int *a, int *b) {
	int temp;

	/* Swapping values using 'temp' variable */
	temp = *a;
	*a = *b;
	*b = temp;
}

int main() {
	int x, y;
	printf("Enter a value for X:\n");
	scanf("%d", &x);
	printf("Enter a value for Y:\n");
	scanf("%d", &y);
	printf("Before swapping\n");
	printf("X = %d\nY = %d\n",x, y);

	/* Passing reference(memory address) of the variables */
	swap(&x, &y);
	printf("After swapping\n");
	printf("X = %d\nY = %d\n",x, y);
	return 0;
}



/* Output */
Enter a value for X:
5

Enter a value for Y:
10

Before swapping
X = 5
Y = 10

After swapping
X = 10
Y = 5
C programmingswapping numberscall by reference

Related Examples

Mashable is a global, multi-platform media and entertainment company For more queries and news contact us on this
Email: info@mashablepartners.com