cocacrave cocacrave - 6 months ago 32
C Question

C Assignments In IF Statement

Sorry if this is a stupid question but I came across this line of code written in C that confuses me coming from JavaScript background.

short s;
if ((s = data[q])) return s;

Is this assigning s to data[q] and if it equals true/1 return s?


Yes, an assignment...well assigns...but it's also an expression. Any value not equalling zero will be evaluated as true and zero as false.

it would be the same as

if ((s = data[q]) != 0) return s;