LearnFrenzy
Sign in Take a free test

Pointers · Predict the Output · Question 11

Function pointer output question in C Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }

int main(void) {
    int (*op)(int, int) = add;
    printf("%d ", op(3, 4));
    op = mul;
    printf("%d\n", op(3, 4));
    return 0;
}
  • A 7 7
  • B 12 12
  • C 7 12 Correct
  • D Compilation error
Correct Answer: Option C

op first points to add, so op(3, 4) is 7. After op = mul, the same call runs mul and gives 12.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Popular