cocacrave cocacrave - 1 year ago 74
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 Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download