Swap two numbers using pointers with C.

#include <stdio.h>

/* Swap function declaration */
void swap(int *num1, int *num2);

int main()
{
    int num1, num2;

    /* Input numbers */
    printf("Enter two numbers: ");
    scanf("%d%d", &num1, &num2);

    /* Print original values of num1 and num2 */
    printf("Before swapping in main n");
    printf("Value of num1 = %d \n", num1);
    printf("Value of num2 = %d \n", num2);

    /* Pass the addresses of num1 and num2 */
    swap(&num1, &num2);

    /* Print the swapped values of num1 and num2 */
    printf("After swapping in main n");
    printf("Value of num1 = %d \n", num1);
    printf("Value of num2 = %d \n\n", num2);

    return 0;
}

/**
 * Function to swap two numbers
 */
void swap(int *num1, int *num2)
{
    int temp;

    // Copy the value of num1 to some temp variable
    temp = *num1;

    // Copy the value of num2 to num1
    *num1 = *num2;

    // Copy the value of num1 stored in temp to num2
    *num2 = temp;

    printf("After swapping in swap function n");
    printf("Value of num1 = %d \n", *num1);
    printf("Value of num2 = %d \n\n", *num2);
}

Output


Enter two numbers : 5 10
Before swapping in main nValue of num1 = 5
Value of num2 = 10
After swapping in swap function nValue of num1 = 10
Value of num2 = 5
After swapping in main nValue of num1 = 10
Value of num2 = 5