Function pointer for arithmetic operations Dry Run in C

Function pointer for arithmetic operations 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.

Function pointer for arithmetic operations Program Code

#include <stdio.h>

int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { return (b != 0) ? a / b : 0; }
int modulo(int a, int b) { return (b != 0) ? a % b : 0; }

int compute(int x, int y, int (*operation)(int, int)) {
    return operation(x, y);
}

int main() {
    int i;
    int a = 25, b = 7;
    int result;
    int (*op)(int, int);
    
    // Array of operation function pointers
    int (*operations[5])(int, int) = {add, subtract, multiply, divide, modulo};
    char *opNames[] = {"Add", "Subtract", "Multiply", "Divide", "Modulo"};
    
    printf("Computations for %d and %d:\n\n", a, b);
    
    for(i = 0; i < 5; i++) {
        result = compute(a, b, operations[i]);
        printf("%s: %d %s %d = %d\n", 
               opNames[i], 
               a, 
               (i==0?"+":i==1?"-":i==2?"*":i==3?"/":"%"), 
               b, 
               result);
    }
    
    // Direct function pointer usage
    printf("\nDirect usage:\n");
    op = add;
    printf("add: %d\n", op(10, 20));
    
    op = multiply;
    printf("multiply: %d\n", op(10, 20));
    
    return 0;
}

View the complete Pointersfunctions programs page.

Program Console Function pointer for arithmetic operations Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.