I have a problem with a question in my book:
#include<stdio.h>
void main()
{
int a=5, b=-7, c=0, d;
d = ++a && ++b || ++c;
printf("\n%d%d%d%d",a,b,c,d);
}
The question asks me what is the output of the code. I ran it and the result on the screen is 6-601. I understand why a=6
and b=-6
, but I don't understand why c=0
and d=1
?
A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, and NOT.
Explanation of logical operator program(a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true). (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false). (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
In MATLAB®, there are three logical operators: & (logical AND), | (logical OR), and ~ (logical NOT). Like the relational operators, they can be used as arithmetical operators and with scalars, matrices and arrays.
There are three logical operators: and , or , and not . The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 and x < 10 is true only if x is greater than 0 and at the same time, x is less than 10.
I believe you already got your answer, but just to elaborate a bit step-by-step, let me add one more clarification here. Firstly, to quote the properties of the &&
and ||
operators, from C11
standard, chapter §6.5.13 and §6.5.13, respectively,
(I)
The
&&
operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0. [...] If the first operand compares equal to 0, the second operand is not evaluated.
and
(II)
The
||
operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. [...]. If the first operand compares unequal to 0, the second operand is not evaluated.
and they both guarantee left-to-right evaluation. So, comparing your code,
d = ++a && ++b || ++c;
it happens like
d = ((++a && ++b) || ++c );
which evaluates to
d = (( 6 && ++b ) || ++c);
and then
d = ( ( 6 && (-6) ) || ++c);
Now in above stage, (I) is fulfilled and it comes down to
d = ( 1 || ++c);
Now, following the emphasis, which already meets the (II), so no further evaluation of the RHS operand of ||
is performed (i.e., ++c
is not evaluated), and it appears to be d = 1
and the final result, 1, is stored into d
.
That's how, a == 6
, b == -6
, c == 0
and d ==1
.
Having said that, void main()
should be changed to int main(void)
, at least to conform with the standard.
The ||
OR operator is short-circuiting, which means that if the left side is true then the right side is not evaluated. In this case ++a && ++b
evaluates to true, so ++c
is never run and c
keeps its value of zero.
Also since it evaluates to true, this is denoted with 1
which is stored in d
.
Any non-zero value is considered to be true and the result of boolean operations is defined to be 0
or 1
as an integer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With