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

Recommended Posts:

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.