C Program to Swap two Numbers

#include <stdio.h>

// This function swaps values pointed by xp and yp
void swap(int *xp, int *yp)
    int temp = *xp;
    *xp = *yp;
    *yp = temp;

int main()
    int x, y;
    printf("Enter Value of x ");
    scanf("%d", &x);
    printf("\nEnter Value of y ");
    scanf("%d", &y);
    swap(&x, &y);
    printf("\nAfter Swapping: x = %d, y = %d", x, y);
    return 0;


Enter Value of x 12

Enter Value of y 14

After Swapping: x = 14, y = 12 

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:

1 Average Difficulty : 1/5.0
Based on 9 vote(s)