C Language study-a=0 and A==0

Source: Internet
Author: User

Explanations of a=0 and a==0:

if: int a = 0;

So a=0 this expression is false, a==0 this expression is true.

if: int a = 1;

So a=0 this expression is false, a==0 this expression is false.

As an example:

    int a = 0;    if (a = 0)        printf ("True");    else        printf ("False");
output: False.

<span style= "FONT-SIZE:18PX;" >   </span><span style= "font-size:10px;" >int a = 0;    if (a = = 0)        printf ("True");    else        printf ("False");</span>
output: True.

    int a = 1;    if (a = = 0)        printf ("True");    else        printf ("False");
Output: False

    int a = 0;    if (a = 1)        printf ("True");    else        printf ("False");
Output: True

An example of combining the C-language precedence with the binding:

int main () {    int a,b,c;    a=b=c=0;    (1>0?a==0:0>1)? b++:c++;    printf ("%d%d%d\n", A, B, c);    return 0;}
The output is: 010.

int main () {    int a,b,c;    a=b=c=0;    (1>0?a=0:0>1)? b++:c++;    printf ("%d%d%d\n", A, B, c);    return 0;}
The output is: 001

int main () {    int a,b,c;    a=b=c=0;    1>0?a++:(0>1?b++:c++);    printf ("%d%d%d\n", A, B, c);    return 0;}
The output is: 100. It is strange that the values of B and C have not changed. In C, conditional expressions: The binding is right-associative (that is, the right-to-left combination). This is the same as the following program:

int main () {    int a,b,c;    a=b=c=0;    1>0?a++:0>1?b++:c++;    printf ("%d%d%d\n", A, B, c);    return 0;}
The parentheses are removed. The output is verified exactly the same: 100.

Then, the precedence of this bracket () is much higher than the precedence of the conditional expression. So it should be calculated first (0>1?b++:c++) this right? Isn't it?

If yes, then 0>1 is false, then execute C + +, so c=1;

If not, then take the back bracket (0>1?b++:c++) as the front?: A subkey of the statement. Then the first Judge 1>0 is true, so execute a++,a=1; answer output 100. Correct!

Is it like this?? Let's verify it again:

int main () {    int a,b,c;    a=b=c=0;    0>1?a++:(0>1?b++:c++);    printf ("%d%d%d\n", A, B, c);    return 0;}
The output is: 001. Analysis, first Judge 0>1 is false, so execution (0>1?b++:c++), judgment, 0>1 for false so execute c++,c=1; output 001, so the hypothesis is verified.

That should be the case at the moment.

Let's look at an example:

int main () {    int a,b,c;    a=b=c=0;    0>1?a++:0>1?b++:c++?1>0?b++:c++:a++;    printf ("%d%d%d\n", A, B, c);    return 0;}


The output is: 101.

C Language study-a=0 and A==0

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.