Calculator using function pointers Dry Run in C

Calculator using function pointers 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.

Calculator using function pointers Program Code

#include <stdio.h>
#include <math.h>

// Basic operations
double add(double a, double b) { return a + b; }
double subtract(double a, double b) { return a - b; }
double multiply(double a, double b) { return a * b; }
double divide(double a, double b) { return b != 0 ? a / b : 0; }

// Advanced operations
double power(double a, double b) { return pow(a, b); }
double modulus(double a, double b) { return fmod(a, b); }
double max(double a, double b) { return a > b ? a : b; }
double min(double a, double b) { return a < b ? a : b; }

typedef double (*MathFunction)(double, double);

typedef struct {
    char *name;
    char symbol;
    MathFunction func;
} Operation;

int main() {
    int i;
    // Table of operations (easy to extend!)
    Operation operations[] = {
        {"Add", '+', add},
        {"Subtract", '-', subtract},
        {"Multiply", '*', multiply},
        {"Divide", '/', divide},
        {"Power", '^', power},
        {"Modulus", '%', modulus},
        {"Max", 'M', max},
        {"Min", 'm', min}
    };
    int opCount = sizeof(operations)/sizeof(operations[0]);
    
    double a, b;
    double result;
    double x = 5, y = 3;
    int choice;
    MathFunction ops[] = {add, multiply, power};
    
    printf("=== Advanced Calculator ===\n\n");
    
    do {
        // Display menu
        printf("\nAvailable Operations:\n");
        for(i = 0; i < opCount; i++) {
            printf("%d. %s (%c)\n", i+1, operations[i].name, operations[i].symbol);
        }
        printf("0. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);
        
        if(choice == 0) {
            printf("Goodbye!\n");
            break;
        }
        
        if(choice >= 1 && choice <= opCount) {
            printf("Enter two numbers: ");
            scanf("%lf %lf", &a, &b);
            
            result = operations[choice-1].func(a, b);
            printf("%.2f %c %.2f = %.2f\n", 
                   a, operations[choice-1].symbol, b, result);
        } else {
            printf("Invalid choice!\n");
        }
    } while(1);
    
    // Demonstrate function pointer flexibility
    printf("\n=== Function Pointer Flexibility ===\n");

    for(i = 0; i < 3; i++) {
        printf("Operation %d: %.0f op %.0f = %.0f\n", i+1, x, y, ops[i](x, y));
    }
    
    return 0;
}

View the complete Pointersfunctions programs page.

Program Console Calculator using function pointers Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.