Skip to main content

Problem Statement

Write a C program with a function that swaps two variables using pointers.

Solution

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int a = 5, b = 10;
    swap(&a, &b);
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

Output

a = 10, b = 5