Question: The code below compiles well

```
int a=5,b=4,c;
a>b?30:40;

```

Also does,

```
int a=5,b=4,c;
a>b?c=30:40;

```

But why this does not work?

```
int a=5,b=4,c;
a>b?c=30:c=40;

```

Answer: 
You are being bitten by precedence. `?:` has very low precedence, but not as low as `=` or `,` (see the [operator precedence table](http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm)).

Your code is parsed as:

```
(a>b ? c=30 : c) = 40;

```

Rather than:

```
a>b ? c=30 : (c=40);

```

You don't need parenthesis around `c=30` because `?` and `:` act like parentheses to the expression within.

---

Believe it or not, `(a>b ? c=30 : c) = 40` is valid C++ (but not valid C). The expression `(a>b ? c=30 : c)` is an [lvalue](https://en.wikipedia.org/wiki/Value_%28computer_science%29) referencing the variable `c`, to which `40` is assigned.
The last one:

```
int a=5,b=4,c;
a>b?c=30:c=40;

```

fails because it's trying to assign `40` to `a>b?c=30:c`, which obviously won't work. The `=` has lower precedence, and `a>b?c=30:c` is a valid expression (though you can't assign to it). The `=` in the `c=30` part is sort of an exception because it's in the middle of the ternary operator, between the `?` and the `:`. To fix it you'd simply need to add parentheses around the `c=40` so that it's evaluated as a single value for the 'else' part of the ternary operator, i.e. `a>b?c=30:(c=40);`

The second example

```
a>b?c=30:40;

```

doesn't assign anything to `c` unless `a` is greater than `b`... which it is when `a` is `5` and `b` is `4`, as in this case; but note that if `a` were not greater than `b`, no assignment would occur.

From the first example

```
a>b?30:40

```

is a valid expression, with a value of `30` or `40`, but you're not doing anything with that value so of course it serves no purpose there.

Of course, you'd normally use something more like:

```
c = a>b ? 30 : 40;

```

where `a>b ? 30 : 40` will evaluate to `30` or `40`, which is then assigned to `c`. But I suspect you know that and simply want to know why the `c=40` is not treated as a single value for the 'else' part of the ternary operator in the last example.