#include <stdio.h>
int main(int argc, char **argv)
{
int i = 0;
int j = 1;
j > i ? printf("1") : printf("0");
return 0;
}
If you had learnt the any programming language, you know the result is 1.
The next words come form The Wikipedia's explanation:
?: is used as follows:
- condition ? value_if_true : value_if_false
The condition is evaluated true or false as a
Boolean expression. On the basis of the evaluation of the Boolean condition, the entire expression returnsvalue_if_true if condition is true, but value_if_false otherwise. Usually the two sub-expressions value_if_true and value_if_false must have the same type, which determines the type of the whole expression. The importance of this type-checking lies in the operator's most common use—in
conditional
assignmentstatements. In this usage it appears as an
expression on the right side of an assignment
statement, as follows:
- variable = condition ? value_if_true : value_if_false
The first expression is condition, so the condition's result only has two values which are true or false. If the condition's result is true, the program will executes the second statement. While the condition's result is false, the program will execute the third statement.
Now, we test the condition statement. We said the condition statement return a bool type value. At C language programming we know the assignment statement must return a true value. But what's the relationship between the assignment statement and ternary operator? You're right. We use assignment statement as the ternary operator's condition statement. This is my code:
#include <stdio.h>
int main(int argc, char **argv)
{
int i = 0;
int j = 1;
j = 0 ? printf("1") : printf("0");
// printf("j = %d", j);
return 0;
}
Do you believe the result is "1"? But the computer's result is "0". Why the computer compute the result is "0"? Is the assignment statement failed? We can add a statement to check the value of j. We add "printf("j = %d", j);". After compiled the source code and executed. The result is j = 1. En...? The assignment statement is really failed. The value of j not changed. We also can change the code as this:
#include <stdio.h>
int main(int argc, char **argv)
{
int i = 0;
int j = 1;
j = 1 ? printf("1") : printf("0");
return 0;
}
Now, we compile and execute it. Oh.Oh. The result is "1". We must confused by this operator. Just now, we concluded that the assignment statement was failed, but now the assignment statement is succeed.
As a C programmer, the operator "=" and "==" is very different. The operator "=" is assignment operator, but the "==" operator is judge the two values if they are equal. In other words, if we replace the "=" as the "==", the execute flow will execute strictly as the Wikipedia's explanation. The test code as follows:
#include <stdio.h>
int main(int argc, char **argv)
{
int i = 0;
int j = 1;
j == 1 ? printf("1") : printf("0");
return 0;
}
This result is "1". If we change the condition with "j == 0", the result is "0".
We continue to talk about assignment statement. The result seems very unsatisfactory. We can guess bravely that is the compile assigned the value to the variable as the result but ignored the assignment. what's the meaning?
#include <stdio.h>
int main(int argc, char **argv)
{
int i = 0;
int j = 1;
j = 0 ? printf("1") : printf("0");
printf("j = %d", j);
return 0;
}
We can use code to understand those words. Just now, we tested the result is "0".
First, the compiler assigned the zero to the variable j and made the j as the condition value. Then, the execute flow base on the condition result to execute the rest statement. Third, the value of j will changed back to the origin value.
<There is a great guess. While the program running, the ternary operator statement been pushed into the stack.Then after the accomplished the ternary operator statement, the stack been destroyed.So the value of j not changed.>
After testing, we know the operator "=" in ternary operator's condition statement, the variable value is the condition result. All this summaries is my own opinion or my own guess because I have not read the compiler develop manual. All this codes tested in Linux gcc 4.7.3 and vs2012.