Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation for this function's output

I am doing review questions which ask me "What is the output of the following," and I am having some trouble understanding something about this function:

int a = 1, b = 1, c = -1;
c = --a && b++;
printf("%d %d %d", a, b, c);

The output is 010. My question is about line 2, c = --a && b++. How is this line processed, and how does it work/change the values? And if it were c = --a || b++? From my understanding I thought the output would be 020.

like image 302
Bobbis Avatar asked Apr 22 '16 08:04

Bobbis


People also ask

How is function output defined?

In mathematics, a function is any expression that produces exactly one answer for any given number that you give it. The input is the number you feed into the expression, and the output is what you get after the look-up work or calculations are finished.

How do you calculate output?

Total output can be measured two ways: as the sum of the values of final goods and services produced and as the sum of values added at each stage of production. GDP plus net income received from other countries equals GNP. GNP is the measure of output typically used to compare incomes generated by different economies.

How can you find an output number when you know an input number?

That rule is: multiply each input number ( -value) by 3 to find each output number ( -value). You can use a rule like this to find other values for this function, too. Now look at how to use a function rule to complete a table.


2 Answers

The key concept to understanding the result is short-circuit evaluation of Boolean operators (&& and ||) -- if, after evaluating the left-hand side of a Boolean operator, the value of the right-hand side cannot affect the overall result, then it will not be evaluated and any side-effects it would produce will not happen.

In the first case, since --a evaluates to 0 (=false) the second part of ... && ... is not evaluated, since "false AND anything" will always be false. Specifically, b++ is never executed, and so its value remains 1 in the output.

In the case of --a || b++, the value of the whole expression cannot be determined by the left-hand side ("false OR something" can still be true) so the b++ is evaluated (and it's side-effect, incrementing b, happens).

The other concept needed to fully understand the results is the difference between pre- and post-increment/decrement operators. If the -- or ++ appears before the variable (as in --a) then the variable is decremented or incremented first and new value is used to evaluate the whole expression. If the -- or ++ appears after the variable (as in b++) then the current value of the variable is used to evaluate the expression and the increment/decrement happens after this has happened.

It should be noted that expressions that try to combine two or more instances of --/++ of the same variable (e.g. a++ + ++a) are quite likely to invoke undefined behaviour -- the result may vary by platform, compiler, compiler and even the time of day.

like image 50
TripeHound Avatar answered Oct 10 '22 07:10

TripeHound


In the expression c = --a && b++, a gets decreased and returned. Now the second argument of the expression --a && b++ is not evaluated because of short circuit evaluation---once we see that --a==0 we already know that the expression will be 0 regardless of what is the other argument---, so b remains unchanged.

Decreased a is 0 and b remains 1.

The output is, as you suggest, 0 1 0.

Regarding the second question, if you write c = --a || b++, the variable a again goes to zero but the expression can still evaluate to true---we must thus evaluate the second part as well, thus executing b++ which returns 1 and increases b. In this case the output would be 0 2 1, because c is assigned the value of 0 || 1 which is 1.

In short, read up on

  • pre- and post-increment in C and C++ and on
  • short circuit evaluation.
like image 37
blazs Avatar answered Oct 10 '22 07:10

blazs