I thought that the ternary operator returns either a value on the left side or the right side of :
depending on the condition. Why does this following piece of code print 1?
#include <stdio.h>
int main(int argc, char const *argv[]) {
int c = 0;
(c?c:0)++;
printf("%i", c);
return 0;
}
You would appear to have a compiler bug, or perhaps a language extension, since this is not valid C. You need an lvalue in order to apply the ++
operator, and (c?c:0)
is not an lvalue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With