User Tools

Site Tools


one_line

This is an old revision of the document!


One line If in C

Often called ternary operators. A really handy way to write condensate and yet easily readable code in C is using one line if when doing only simple operation in the if.

It is absolutely not recommended to use this syntax everywhere but when only incrementing a variable for example, it is great.

Two syntax exists one with an integrated else:

(CONDITION) ? TRUE: FALSE;
syntax1.c
(Variable > Threshold) ? Variable-- : Variable++;

And a simple if,

if (CONDITION) TRUE;
syntax2.c
if (Variable < 0) Variable = 0;
one_line.1537370212.txt.gz · Last modified: 2018/09/19 17:16 by supergnu