LearnFrenzy
Sign in Take a free test

Typedef · Predict the Output · Question 4

A pipeline of typedef'd function pointers – output Answer, explanation & discussion

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

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

typedef int (*Op)(int);

static int inc(int x) { return x + 1; }
static int dbl(int x) { return x * 2; }
static int sq(int x)  { return x * x; }

int main(void) {
    Op pipeline[] = {inc, dbl, sq};
    int v = 3;
    for (size_t i = 0; i < sizeof pipeline / sizeof pipeline[0]; i++)
        v = pipeline[i](v);
    printf("%d\n", v);
    return 0;
}
  • A 64 Correct
  • B 49
  • C 16
  • D 36
Correct Answer: Option A

The functions run in order: 3 + 1 = 4, 4 × 2 = 8, 8² = 64. The typedef makes the array of function pointers read like any other array.

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.

Search LearnFrenzy

Esc

Quick links