LearnFrenzy
Sign in Take a free test

Decision Making · Predict the Output · Question 3

Which functions run? A short-circuit trace Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

static int check(const char *name, int result) {
    printf("%s ", name);
    return result;
}

int main(void) {
    if (check("A", 0) && check("B", 1)) { }
    if (check("C", 1) || check("D", 1)) { }
    if (check("E", 1) && check("F", 0) || check("G", 1)) { }
    printf("\n");
    return 0;
}
  • A A C E F G Correct
  • B A B C D E F G
  • C A C E F
  • D A B C E F G
Correct Answer: Option A

A is false, so B is skipped. C is true, so D is skipped. The third line is (E && F) || G: E true → F runs and is false → the || needs G.

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