ProgrammingVery hardProduct company OA · GATE CS

With int a = 3, b = 7; which of these statements compile as C (not C++)?

With int a = 3, b = 7; which of these statements compile as C (not C++)?
*(a > b ? &a : &b) = 0;      /* (i)   */
(a > b ? a : b) = 0;         /* (ii)  */
a > b ? a : b = 0;           /* (iii) */
a > b ? (a = 0) : (b = 0);   /* (iv)  */
  1. A. (i) and (iv) only
  2. B. (i), (iii) and (iv) only
  3. C. All four
  4. D. (i) only

Correct answer: A. (i) and (iv) only

In C the result of ?: is never an lvalue, so (ii) cannot be assigned to. C's grammar also parses (iii) as (a > b ? a : b) = 0, so it fails the same way; only C++ reads it as a > b ? a : (b = 0). (i) picks an address and dereferences it, and (iv) just puts assignments inside the branches, so both compile.

More C Programming questions

Exams that ask this topic