C Tutorial
This c program defines a call by reference swap function which is used to swap two integer variables value.
Swapping variable data type can be float, character as well. this example is based on integer variables.
#include<stdio.h> // Swap function declaration void swap(int*, int*); void main() { int a, b; printf("Enter a: "); scanf("%d", &a); printf("Enter b: "); scanf("%d", &b); printf("Before swap: a = %d, b = %d\n", a, b); swap(&a, &b); printf("After Swap: b = %d, b = %d\n", a, b); } //Swap function definition void swap(int *a, int *b) { int temp; temp = *b; *b = *a; *a = temp; }Output:
$ cc swap.c $ ./a.out Enter a: 34 Enter b: 54 Before swap: a = 34, b = 54 After Swap: b = 54, b = 34
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page