29. Pointer- Swap Value

#include <stdio.h>

// Function to swap two integers using pointers
void swap(int *p1, int *p2) {
    int temp = *p1;   // Store value at p1
    *p1 = *p2;        // Assign value at p2 to p1
    *p2 = temp;       // Assign temp to p2
}

int main() {
    int a, b;
    scanf("%d %d", &a, &b);

    // Pass addresses to swap function
    swap(&a, &b);

    // Print swapped values
    printf("a = %d ", a);
    printf("b = %d", b);

    return 0;
}

 

 

Loading...

Input

10 20

Expected Output

a = 20 b = 10