cocacrave cocacrave - 3 months ago 9
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?

Answer

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;
Comments