4

I get "error: lvalue required as left operand of assignment" for "x=k" in C, but the code runs without an error in C++. I don't understand why C is giving me this error, while C++ doesn't.

#include <stdio.h>
int main() { 
    int j=10, k=50, x;
    j<k ? x=j : x=k; 
    printf("%d",x);
}
dbush
  • 205,898
  • 23
  • 218
  • 273
starnairrr
  • 59
  • 3
  • 13
    Because C and C++ are different languages – Pepijn Kramer Aug 08 '22 at 15:27
  • 3
    Well in general, C is not C++. For the C version, you would want `x = (j < k) ? j : k;` – Rogue Aug 08 '22 at 15:27
  • 3
    why not just `x = j < k ? j : k; // ?` – nurettin Aug 08 '22 at 15:27
  • 3
    @Rogue yes C and C++ are different, but sometimes the differences aren't obvious. Understanding those differences helps us all. – Mark Ransom Aug 08 '22 at 15:30
  • 4
    Because, in C++, `?:` has the same precedence as assignment operators, but in C, it has higher precedence. So, in C++, `j – Peter Aug 08 '22 at 15:36
  • @MarkRansom I disagree, C++ is based on an ancient version of C and still supports some things. You really should treat them as different languages. Using current C++ guidelines should produce very different code from what "C" can do. All that the differences show us IS that they are different language and IMO should be treated as such. – Pepijn Kramer Aug 08 '22 at 15:36
  • dup: https://stackoverflow.com/a/1082682/4641116 – Eljay Aug 08 '22 at 15:44
  • @PepijnKramer they may be different languages but they share the same background, and 99% of the time the parts that they share work the same. I still assert that knowing those differences is useful. – Mark Ransom Aug 08 '22 at 15:52
  • It is useful C++ can still compile legacy code, but new C++ code should look remarkably different from new C code. (RAII, STL containers i.o. malloc/free, new/delete, unique pointer ownership, references, exception safe programming techniques etc. etc.). So lets respectfully disagree then :) And yes part of my view comes from the fact I don't need to use those 2 languages at the same time. – Pepijn Kramer Aug 08 '22 at 16:06
  • Exact duplicate of: [Ternary operator in C vs C++](https://stackoverflow.com/questions/28337054/ternary-operator-in-c-vs-c) – Jason Aug 08 '22 at 16:08
  • @PepijnKramer *"C and C++ are different languages"* That's all that need to be said – klutt Aug 08 '22 at 16:25

1 Answers1

11

In C, the ternary operator ?: has higher precedence than the assignment operator =. So this:

j<k ? x=j : x=k;

Parses as this:

((j<k) ? (x=j) : x)=k;

This is an error in C because the result of the ternary operator is not an lvalue, i.e. it does not denote an object and so can't appear on the left side of an assignment.

C++ however has ?: and = at the same precedence level, so it parses the expression like this:

j<k ? x=j : (x=k);

Which is why it works in C++. And actually, C++ does allow the result of the ternary operator to be an lvalue, so something like this:

(j<k ? a : b ) = k;

Is legal in C++.

You'll need to add parenthesis to get the grouping you want:

j<k ? x=j : (x=k);

Or better yet:

x = j<k ? j : k;
dbush
  • 205,898
  • 23
  • 218
  • 273