int qempty()
{
return (f == r ? 1 : 0);
}
In the above snippet, what does "?" mean? What can we replace it with?
This question is related to
c++
operators
ternary-operator
It's the conditional operator.
a ? b : c
It's a shortcut for IF/THEN/ELSE.
means: if a is true, return b, else return c. In this case, if f==r, return 1, else return 0.
The question mark is the conditional operator. The code means that if f==r then 1 is returned, otherwise, return 0. The code could be rewritten as
int qempty()
{
if(f==r)
return 1;
else
return 0;
}
which is probably not the cleanest way to do it, but hopefully helps your understanding.
This is a ternary operator, it's basically an inline if statement
x ? y : z
works like
if(x) y else z
except, instead of statements you have expressions; so you can use it in the middle of a more complex statement.
It's useful for writing succinct code, but can be overused to create hard to maintain code.
You can just rewrite it as:
int qempty(){ return(f==r);}
Which does the same thing as said in the other answers.
Just a note, if you ever see this:
a = x ? : y;
It's a GNU extension to the standard (see https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html#Conditionals).
It is the same as
a = x ? x : y;
It is called the conditional operator.
You can replace it with:
int qempty(){
if (f == r) return 1;
else return 0;
}
Source: Stackoverflow.com