ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
int main(void) {
    int x = 5, y = 5;
    if (x = y)
        printf("true:%d", x);
    else
        printf("false:%d", x);
}
  1. A. false:5
  2. B. Compilation error: assignment in condition is not allowed
  3. C. true:1
  4. D. true:5

Correct answer: D. true:5

`x = y` is an ASSIGNMENT (not a comparison, a classic = vs == typo trap), which assigns 5 to x and evaluates to that value, 5, which is truthy; the if-branch runs, and x is still 5, so "true:5" prints.

More C Programming questions

Exams that ask this topic