Talk C Chestnut Bar (163th: C-language example-three mesh operator)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about the socket Knowledge System diagram example, this time we say the example is: three mesh operator . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, we summarized and summed up the sockets in the previous chapter, thus ending the introduction of sockets. In this one we will begin to introduce a new knowledge, which is the three-mesh operator.

Note to the three mesh operator, some crossing may not know what it is, and if I write a sign, everyone must know what it is "? :”。 Ah! It turns out it is. There has been an exclamation of crossing. In fact, sometimes I will be the same as everyone, only know how to use, but do not know what it is called, haha.

Let's introduce the trinocular operator today, to let you know its official name, and to introduce some of its usage.

  • standard format for three-mesh operators : Expr1? Expr2:expr3. Where expr represents an expression, and of course it can be a variable.
  • operation procedure for the three-mesh operator : If the value of Expr1 is true, then the result of the three-mesh operator is the value of EXPR2, otherwise it is the value of EXPR3.

We then use the specific code to illustrate:

#include <stdio.h>intMain () {intA,b,res; A =3; b =5; res =0;printf("There is Value,they are:a=%d , b=%d \ n", A, b); res = (a>b)? a:b;//Find the maximum value in two valuesif(RES)printf("The MAX value of them is: %d \ n", res); b =0; res =0;printf("There is Value,they are:a=%d , b=%d \ n", A, b); res = (b==0)?0:(A/b);//Before the division operation, determine whether the value of the divisor is0    if(RES)printf("the/operation result is: %d \ n", res);Else        printf("can not make/operation \ n");return 0;}

As we can see from the above code, the first time we use the three mesh operator, we use it to calculate the maximum of two values, at which point the expr1 in the trinocular operator is an expression: a>b, EXPR2 and EXPR3 are variables A and B, respectively. When we use the three-mesh operator for the second time, we use it for division operations. To improve the robustness of the code, determine whether the divisor value is 0 before the division operation. At this point, the expr1 in the trinocular operator is an expression: B==0,EXPR2 is a numeric 0;EXPR3 is also an expression: A/b.

Crossing, here are the results of the operation of the program, please refer to:

twovaluea=3 , b=5thevalueof5twovaluea=3 , b=0not

Crossing, in actual engineering, we often use the trinocular operator to make some simple and judgmental decisions, and then to replace the If/else judgment statement. Because this approach can improve the simplicity of the code.

You crossing, for example of the trinocular operator let's talk about this. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (163th: C-language example-three mesh operator)

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.