What is the use of trinomial operators in C language
What is the use of the ternary operator in C language? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Generally speaking, the ternary operator in C language is a?b:c, that is, there are three quantities involved in the operation.
The general form of a conditional expression consisting of conditional operators is: expression 1? Expression 2: expression 3 evaluates as follows: if the value of expression 1 is true, the value of expression 2 is taken as the value of the conditional expression, otherwise the value of expression 2 is taken as the value of the entire conditional expression. Conditional expressions are usually used in assignment statements. For example, conditional statements:
If (a > b) max=a;else max=b; can be written as max= (a > b)? a max using conditional expressions; the semantics of executing this statement are: if a > b is true, assign a to max, otherwise assign b to it. When we use conditional expressions, we should also pay attention to the following points: (1) the operation priority of conditional operators is lower than that of relational operators and arithmetic operators, but higher than that of assignment operators. So max= (a > b)? a b?a:b b can remove the parentheses and write as the conditional operator max=a > ARV (2)? And: are a pair of operators and cannot be used separately. (3) the binding direction of conditional operators is from right to left. For example: a > b?a:c > d?c:d should be understood as a > bounded a: (c > d?c:d) this is the case where conditional expressions are nested, where expression 3 is another conditional expression. Void main () {int aforamagram printf ("\ n input two numbers:"); scanf ("% d% d", & aPowerprintb); printf ("max=%d", a > b?a:b); / / for "what is the ternary operator", it can be understood as if () statement one; else statement two; a? B: c}
After reading the above, have you mastered the method of ternary operator in C language? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!