Reputation:
Conditional operator in C is used like this:
condition ? value_if_true : value_if_false
What does 0 mean when it's used in the value_if_false
?
I've seen some people using it like this, for example.
a == b ? i++ : 0
It seems like it does nothing. Does this work like return 0
in other functions?
Upvotes: 2
Views: 992
Reputation: 15052
The reason why someone might want to write a == b ? i++ : 0;
is that s/he probably wants to have an (Caution! You are now entering an opinion-based area) easier and faster alternative to if (a == b) i++;
- although this is of course opinion-based and I personally not share the same opinion.
One thing I can think of as a "blocker" at the if
statement is the requirement to write the parentheses ()
which can be omitted by using the conditional operator instead.
"But why the 0
?"
The C syntax requires a third operand for the conditional operator. Else if you would want to compile for example:
a == b ? i++;
you will get an error from the compiler:
"error: expected
':'
before';'
token"
Or respectively, doing so:
a == b ? i++ : ;
would raise:
"error: expected expression before
';'
token"
So they use 0
as kind of "syntax satisfier" to be able to use the conditional operator as replacement for the if
statement. You could use any other numeral value here as well, but 0
is the most readable value, which signifies that it has no use otherwise.
To showcase the use at an example:
#include <stdio.h>
int main (void)
{
int a, b, c = 4;
a = 2;
b = 2;
a == b ? c++ : 0;
printf("%d",c);
return 0;
}
The output for c
will be 5
, because a == b
.
Note that a == b ? i++ : 0
is different when used f.e. inside of an assignment like f.e.:
int c = a == b ? i++ : 0;
Here c
is either getting assigned by i
or 0
, dependent upon a == b
is true or not. If a == b
is true, c
is assigned by i
. If a == b
is wrong, c
is assigned by 0
.
Side Notes:
To view it from a technical point, ?=
is called the "conditional operator". The conditional operator is one of the group of ternary operators.
If you want to learn more about the conditional operator ?=
, look at ISO:IEC 9899:2018 (C18), §6.5.15 - "Conditional operator" for more information.
Upvotes: 1
Reputation: 18410
The ?:
operator is a ternary operator, but it is not called "ternary" as some answers and/or comments here suggest. It just is the arity of the operator, just as +
is a binary operator or as &
is unary. If it has a name at all, it is called "Conditional Expression"-operator
It is not quite equivalent to if/else, because it is a conditional value (with the consequence, that both expressions must have the same type) in the first place, not a conditional execution. Of course, both types can be cast to make them equal.
In the case of what the OP does, a better option (if if
shall not be used) is in my opinion:
a == b && i++;
which resembles a bit more logical what happens. But of course it is a matter of style.
Upvotes: 1
Reputation: 7441
In C language, ternary is shorter version of if statement and it requires both statements, if_true
and if_false
. It would be like this (in fact it can have multiple statements for one case, separated with comma):
Short:
condition ? if_true : if_false;
Long:
if (condition) {
if_true;
} else {
if_false;
}
You can also assign the value if you put something infront of condition
.
Short:
result = condition ? if_true : if_false;
Long:
if (condition) {
result = if_true;
} else {
result = if_false;
}
Now here is the trick. In C language, writing 0;
is a valid statement, so your ternary becomes in longer version same as code below:
if (a == b) {
i++;
} else {
0; /* This is valid C statement */
}
Or if you have assignment too, it would be:
if (a == b) {
result = i++;
} else {
result = 0;
}
You can also do this:
int a;
/* Code here ... */
condition ? a = 5: 0;
That is effectively the same as:
if (condition) {
a = 5;
} else {
/* DO not touch a */
}
Upvotes: 2
Reputation: 1542
It helps to think of the ternary operator as a shorthand way or writing an if-else statement.
If(a == b){
i++;
}else{
//if assigned, return 0. Else do nothing.
}
Upvotes: 0
Reputation: 7271
There's nothing special about 0
one could write
a == b ? i++ : 1
And it would behave the same way.
Only difference is when you assign the expression to say another variable:
int c = a == b ? i++ : 1;
// a == b -> c will be i++
// a != b -> c will be 1
However it's much cleaner to write
if (a == b) i++;
Upvotes: 0