[Study Notes] [C Language] Three-object operator, learning note Operator

Source: Internet
Author: User

[Study Notes] [C Language] Three-object operator, learning note Operator

1. N object Operator

Like logical non (!) A negative sign (-) that connects only one data. It is called a single-object operator, such! 5.-5.
A negative number that connects two data, such as arithmetic operators, Relational operators, and logical operators, is called a binary operator ", for example, 6 + 7, 8*5, 5> 6, 4 & 0,
Similarly, operators that connect three data are called "Three-object operators"

 2. Three-object Operator

The C language provides a unique three-object OPERATOR: Conditional operator.
1> Format

Expression? Expression B: expression C
2> operation result

If expression A is true, the result of the conditional operator is the value of expression B. Otherwise, it is the value of expression C.
3> integration direction and priority

The priority is: Arithmetic Operators> Relational operators> conditional operators> value assignment operators.
The combination direction of conditional operators is "from right to left"

int a = 3>4 ? 4+5 : 5>4 ? 5+6 : 6>7+1;

The above code is equivalent

int a = (3>4) ? (4+5) : ( (5>4) ? (5+6) : (6>(7+1)) );

To simplify the process

int a = 0 ? 9 : ( 1 ? 11 : 0 );

Simplified

int a = 0 ? 9 : 11;

So the value of a is 11.

 

 

1 // What are the three-object operator conditions? Value 1: Value 2 2 3 // int a =! 100? 9: 89; 4 5 // printf ("a = % d \ n", a); 6 7 8 # include <stdio. h> 9 10 int main () 11 {12/* calculate the maximum value between two integers 13 int a = 10; 14 15 int B = 99; 16 17 int c = a> B? A: B; 18 19 printf ("c is % d \ n", c); 20 */21 22 // calculate the maximum value between the three integers 23 int a = 10; 24 int B = 999999; 25 int c = 1000; 26 27 // obtain the maximum value of a and B 28 int abMax = (a> B )? A: B; 29 // obtain the final maximum value 30 int d = (abMax> c )? AbMax: c; 31 32 // int d = (a> B )? A: B)> c )? (A> B )? A: B): c; 33 34 printf ("d is % d \ n", d); 35 return 0; 36}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.