I can not understand the output of this programming. Please, help

#include <stdio.h> int main () { int x, y, z; x = y = z = 1; ++x || ++y && ++z; printf ("x = %d\ty = %d\tz = %d\n", x, y, z); //op : x = 2 y = 1 z = 1 //why is 'x' only incrementd? x = y = z = -1; ++x || ++y && ++z; printf ("x = %d\ty = %d\tz = %d\n", x, y, z); //op : x = 0 y = 0 z = -1 //why are 'x' and 'y' incremented? x = y = z = 1; ++x && ++y || ++z; printf ("x = %d\ty = %d\tz = %d\n", x, y, z); //op : x = 2 y = 2 z = 1 //why is 'x' only incrementd? x = y = z = -1; ++x && ++y || ++z; printf ("x = %d\ty = %d\tz = %d\n", x, y, z); //op : x = 0 y = -1 z = 0 //why are 'x' and 'z' incremented? //Does this incrementation depend on the value stored in the variable? } 
+4
source share
6 answers

|| and && short circuit. This means that they do as little work as possible in order to regain their value, only performing the right side, if the left side does not nail the answer.

For instance:

 1 || anything(); 

In this case, nothing () will ever be executed because || may just return as soon as he rates 1; no matter what () returns a value, the return value || there can never be 0 in this expression.

Similarly:

 0 && anything_else(); 

Here Nothing_else () will never be executed, because && already knows that its value can never be anything but 0.

In your examples, preincrements ++ does not actually affect the short circuit, except to hide the values ​​that the Boolean short circuit operators actually assume.

+5
source

The reason is that the && and || short circuit .

That is, as soon as they find out what the final result of the Boolean expression is, they cease to evaluate.

So, if you execute 1 || x++ 1 || x++ , then x will never increase, because any nonzero Boolean value is a true value.

Similarly, if you execute 0 && x++ , x++ will never be executed.

See also:

+9
source

In general, the answer to your questions is that if you have the code A||B If A evaluates to true, then B is NEVER evaluated.

So, in your first case, if ++ x is true (in your case x == 2, so that's true), then the rest of the expression is never evaluated

Similarly, in the second case, ++ x calculates 0, which is false, so you need to evaluate the second art of expressing ++ y (and this is also 0, which is false); no in the same way as || if the first operand of the && operator is false, then there is no need to evaluate the second operand - thus, the third term is never evaluated.

The same logic can be applied to the third case.

+1
source

This is due to the fact that c closes the closures of the operators || and && .

For example, if you have A && B , the program will evaluate A If this turns out to be false, we already know that the result will be false regardless of what is in B , so B never evaluated. If you are C || D C || D and C evaluate to true, you know that the result is true no matter what is in D , so D never evaluated.

+1
source

In this code, variable increments are used in conjunction with expressions. This is to avoid using an if-else structure.

Here are examples that clarify operator priorities:

 ++x || (++y && ++z); ++x || (++y && ++z); (++x && ++y) || ++z; (++x && ++y) || ++z; 

expressions are evaluated from left to right. In the first example, the program already knows that the whole expression will be returned true after ++ x, and the program will not bother to evaluate the rest of the expression. C uses what is called a short circuit rating.

+1
source

In C ++ or C:

True - 1 or any nonzero value

False - 0 or any negative value

These conditions are short-circuited - therefore, when C ++ / C encounters the first true part - it breaks and does not evaluate the rest.

 x = y = z = 1; ++x || ++y && ++z; printf ("x = %d\ty = %d\tz = %d\n", x, y, z); //op : x = 2 y = 1 z = 1 //why is 'x' only incrementd? 

x is true (2) and the rest of the condition is not evaluated

 x = y = z = -1; ++x || ++y && ++z; printf ("x = %d\ty = %d\tz = %d\n", x, y, z); //op : x = 0 y = 0 z = -1 //why are 'x' and 'y' incremented? 

x is false (0), so a calculation of y that is false (0) is performed - since it is false, it is not yet verified further.

 x = y = z = 1; ++x && ++y || ++z; printf ("x = %d\ty = %d\tz = %d\n", x, y, z); //op : x = 2 y = 2 z = 1 //why is 'x' only incrementd? 

x is true (2, which is not equal to zero) y is true (2) - since x and y are true - the first part is true, therefore it does not evaluate z.

 x = y = z = -1; ++x && ++y || ++z; printf ("x = %d\ty = %d\tz = %d\n", x, y, z); //op : x = 0 y = -1 z = 0 //why are 'x' and 'z' incremented? 

evaluated as (x & y) || Z x is evaluated and false (0) z is then evaluated and false (0), the result of this condition is actually incorrect.

I may be completely wrong, but as I understand it. Hope this helps.

0
source

All Articles