Statement with? in C

Possible duplicate:
How to use a conditional statement?

I am new to C, and in one example code that I was looking at, I came across a statement:

A = A ? B: C[0]

I am just wondering what the task of the previous operator is and what will be the result after executing the mentioned operator.

+5
source share
6 answers

It is called a ternary operator . expr ? a : breturns aif exprtrue b, if false. exprcan be a boolean expression (e.g. x > 3), a boolean literal / variable, or any that can be written in a boolean (e.g. int).

int ret = expr ? a : b :

int ret;
if (expr) ret = a;
else ret = b;

, , , , . , , ret = (expr ? a : b) > 0;

Python >= 2.6 : a if expr else b.

+13

A B, A , C[0].

?:

+4

result = a > b ? x : y; :

if (a > b) {
  result = x;
}
else
{
  result = y;
}
+3

, if else.

:

if ( A != 0 )
{
    A = B;
}
else
{
    A = C[ 0 ];
}
+3

A is assigned to B if A (non-NULL) exists, otherwise C [0].

+1
source

if A is 0, then A = C [0] else A = B

+1
source

All Articles