ricky162 ricky162 - 4 months ago 12
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

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;
}
else
{
    return z;
}
Comments