Machine-translated by Google. Questions are authored in English.
Top

Discussion

Point out the error, if any in the program.

#include <stdio.h> 
int main()
{
    int a = 10, b;
    a >=5 ? b=100: b=200;
    printf("%d\n", b);
    return 0;
}

 

  • A.100
  • B.200
  • C.Error: L value required for b
  • D.Garbage value

Answer: C

Correct answer: Error: L value required for b

Assignment binds more loosely than the conditional operator, so the line parses as

(a >= 5 ? b = 100 : b) = 200;

and the left-hand side is now a conditional expression, which is a value rather than an object - so it cannot be assigned to. Written as b = a >= 5 ? 100 : 200; it would compile and give 100.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.