Meenu said:
In the following program I can't understand the value of k
#include<stdio.h>
int main()
{
int i=-3, j=2, k=0,m;
m= ++i&&++j||++k;
C logical operators use what is called "short-circuit" evaluation; if
the result of the first operand determines the result of the whole
expression, the second operand isn't even evaluated.
For example, given the expression
a && b
if a evaluates to 0 (false), then the whole expression will evaluate to
false regardless of the value of b, so b isn't evaluated at all.
Similarly, given
a || b
if a evaluates to non-zero (true), then the whole expression will
evaluate to true regardless of the value of b, so b isn't evaluated at
all.
Logical operator precedence is such that && expressions are evaluted
before || expressions, so your statement above is evaluated as
(++i && ++j) || ++k
since the results of both ++i and ++j are non-zero, the expression (++i
&& ++j) evaluates to true, so the whole expression (++i && ++j) || ++k
will evaluate to true regardless of k, so ++k is never evaluated, so k
remains 0.
printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);
return(0);
}