Logical operators' precedence in C

  • A+
Category:Languages
#include <stdio.h>  int main(void) {     int a = 0, b = 0, c = 0;     ++a || ++b && ++c;     printf("%d %d %d", a, b, c);     return 0; } 

The outputs are 1, 0, 0 by gcc 8.1.0. The &&‘s precedence should higher than ||.

Why are the b and c are still 0?

 


The expression ++a || ++b && ++c is grouped as ++a || (++b && ++c). But, the right hand side of || is only evaluated if ++a is 0, which it isn't.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: