Swap numbers using function (pointer) Dry Run in C

Swap numbers using function (pointer) is an interactive C dry run visualizer from the Pointersfunctions programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Swap numbers using function (pointer) Program Code

#include <stdio.h>

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

// Failed swap - demonstrates call by value limitation
void badSwap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 10, y = 20;
    
    printf("Demonstrating bad swap (call by value):\n");
    printf("Before badSwap: x=%d, y=%d\n", x, y);
    badSwap(x, y);
    printf("After badSwap: x=%d, y=%d (NO CHANGE!)\n", x, y);
    
    printf("\nDemonstrating correct swap (call by reference):\n");
    printf("Before swap: x=%d, y=%d\n", x, y);
    swap(&x, &y);
    printf("After swap: x=%d, y=%d\n", x, y);
    
    // Swap again to restore
    swap(&x, &y);
    
    printf("\nPointer values during swap:\n");
    printf("Address of x: %p, Value: %d\n", &x, x);
    printf("Address of y: %p, Value: %d\n", &y, y);
    
    return 0;
}

View the complete Pointersfunctions programs page.

Program Console Swap numbers using function (pointer) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.