ricky162 ricky162 - 1 year ago 133
C Question

What does return z < 0 ? z + y : z mean?

I came across a different way of returning values and got excited.
What does it mean exactly?

If I had to guess, I would say it means something along the line of...
if z < 0, then add z and y, else return z?

int ffunction(int x, int y)
int z = x % y;
return z < 0 ? z + y : z;

Answer Source

You're using the ternary operator and returning the result of that. It's basically an if statement on one line. It's saying:

if(z < 0)
    return z + y;
    return z;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download