Question discussion

What is the output of the following C program?#include int main() { int a = 0, b = 1; if (a) if (b) printf("X"); else printf("Y"); printf("…

2586Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C program?

#include <stdio.h>
int main() {
    int a = 0, b = 1;
    if (a)
        if (b)
            printf("X");
        else
            printf("Y");
    printf("Z");
    return 0;
}
  • AYZ
  • BXZ
  • CZ
  • DY
Correct answer: C

Explanation

An else always belongs to the nearest unmatched if, so it pairs with if (b); since a is 0 the whole inner statement, including that else, is skipped and only Z is printed.

Discussion 0

Nobody has replied yet. Start the discussion!