Value of k??

M

Meenu

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;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}
 
N

noblesantosh

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;

/* if left expression of || results in true, right expression will not
be evaluated */
printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}
 
J

John Bode

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);
}
 
C

Cong Wang

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;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}
Er,yeah.k=0.Just because ++i is TRUE and ++j is TRUE,so ++i&&++j is
TRUE of course.The compiler is lazy!If computes x||y and x is TRUE,it
does NOT compute y at all!So ++k isn't computed.(EOF)
 
C

Cong Wang

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;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}
PS:'return' is keyword,not operator.() is NOT neccessary.
 
M

Meenu

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;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}

and what about
m= ++i||++j&&++k;
 
S

Simon Morgan

and what about
m= ++i||++j&&++k;

Cong just explained how it works. If you can't apply his explanation to
another (practically identical) expression then there's no hope for you.
 
J

John Bode

Meenu said:
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;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}

and what about
m= ++i||++j&&++k;

If ++i evaluates to a non-zero (true) value, then the expression (++j
&& ++K) doesn't need to be evaluated, so neither ++j nor ++k are
evaluated.
 
T

Targeur fou

Meenu a écrit :
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;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}

and what about
m= ++i||++j&&++k;


i equals -3 at the beginning, then -2 so "++i" is true. It's not
necessary to go further in the evaluation of the entire expression,
i.e. j and k aren't incremented. It has already been explained by
others.
m = 1 (strictly, it's something different of zero (true)).
i = -2, j = 2, k = 0.

Please read your C book or lessons to know more about expressions, the
priority of operators and sequence points in C. It seems that you
expect quick answers without searching by yourself for some homework.

Regis
 
M

Mark

Cong Wang 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;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}
PS:'return' is keyword,not operator.() is NOT neccessary.

The return STATEMENT doesn't require parens because it is not a function.
Your statement implies that 'operators' require parenthesis... that is
incorrect.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,167
Messages
2,570,911
Members
47,453
Latest member
MadelinePh

Latest Threads

Top