Alex Rivera | Logout

What does the question mark character ('?') mean in C++?

Asked 2009-04-27T21:07:33.407
115
int qempty()
{
    return (f == r ? 1 : 0);
}

In the above snippet, what does "?" mean? What can we replace it with?

Edit
Report

2 Answers

3

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.

answered 2009-04-27T21:10:40.887
2

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.

answered 2009-04-27T21:14:07.323

Your Answer