C conditional operator in GCC -
from "21st century c" example 10.8
typedef struct { double width, height; } size_s; size_s width_height(char *papertype){ return !strcasecmp(papertype, "a4") ? (size_s) {.width=210, .height=297} : !strcasecmp(papertype, "letter") ? (size_s) {.width=216, .height=279} : !strcasecmp(papertype, "legal") ? (size_s) {.width=216, .height=356} : (size_s) {.width=nan, .height=nan}; }
trying use style in winavr gcc
(a_t_g.value.ax > xx) ? { leds |= rpr; //roll positive red leds &= ~rpa; //clear amber flag |= flagrdd; //set flag red } : (a_t_g.value.ax < -xx) ? { leds |= rnr; //roll negative red leds &= ~rna; //clear amber flag |= flagrdd; //set flag red } :{}
this results in error: expected expression before '{' token
.
can gcc not handle or doing wrong? how 1 handle final nothing in style?
the ternary ?:
operator use in expressions. both branches need expression value; doesn't make sense have empty block. note using compound blocks in expressions gcc extension, 1 there's no reason using here.
the way work use plain if
, else
.
if (a_t_g.value.ax > xx) { leds |= rpr; //roll positive red leds &= ~rpa; //clear amber flag |= flagrdd; //set flag red } else if (a_t_g.value.ax < -xx) { leds |= rnr; //roll negative red leds &= ~rna; //clear amber flag |= flagrdd; //set flag red }
Comments
Post a Comment