Logic or with logic and traps

Logic and logic with the slightest mistake or we will fall into his trap;
Next we look at this Code;

#include<stdio.h>
int main()
{
int i=0;
int a = 0;
int b = 2;
int c = 3;
int d = 4;
i = a++ && ++b && d++;
printf("a = %d\n,b = %d\n,c = %d\n,d = %d\n",a,b,c,d);
return 0;
}

If the calculation result is granted 1335; Sure then fell into his trap;
In this expression, i is calculated in order that he is such;

i = a++ && ++b && d++;
   ( 0  && ++b && d++)

Logic calculates logical AND and equivalent means, and any number of logical 0 and the all 0, so the expression ++ b not performed during the execution of the program;

i = a++ && ++b && d++;
   (     0     && d++)

Similarly, d ++ This expression did not appear to be performed; after such a code is run up result: 1234;

Then the logic or among:

#include<stdio.h>
int main()
{
int i=0;
int a = 1;
int b = 2;
int c = 3;
int d = 4;
i = a++ || ++b || d++;
printf("a = %d\n,b = %d\n,c = %d\n,d = %d\n",a,b,c,d);
return 0;
}

In the expression i's; the logic or the equivalent in mathematics we are talking about or;

i = a++ || ++b || d++;
   ( 1 || ++b || d++);

Because 1 is true, the logic is performed all the time, or true, so this expression ++ b is not performed, similarly also did not perform d ++ implementation; then the results of the code is run up 2234;

Published 20 original articles · won praise 9 · views 912

Guess you like

Origin blog.csdn.net/weixin_44915811/article/details/90417381