07 July, 2011

Ternary Operator

main()
{
int a = 10, b = 7;
a >= 5 ? b = 100 : b = 200;
printf("B = %d\n",b);
}
When we compile this prg, I am getting an error like this:
3: error: lvalue required as left operand of assignment

What does this mean and how to resolve it?

2 comments:

  1. try this

    main()
    {
    int a = 10, b = 7;
    b= (a >= 5) ?100 : 200;
    printf("B = %d\n",b);
    }

    ReplyDelete
  2. @mathew: this will work fine, we know it.. the error is coming from ternary operator statment.. if you replace the statement like this "a>=5 ? b = 100 : 200", this will compile without any errors(lets not bother about o/p)..
    so just want to know what is wrong with the original statement.....

    ReplyDelete