Click here to Skip to main content
15,911,503 members
Please Sign up or sign in to vote.
1.57/5 (3 votes)
See more:
C
int main()
{

int i=-3,j=2,k=0,m;
m=++i&& ++j||++k;
printf("%d%d%d%d",i,j,k,m);
return 0;

}

My answer is

i=-2
j=3
k=1
m=1

Actual answer
i=-2
j=3
k=0
m=1

Question how this k comes to 0(zero)
Posted
Updated 17-Jul-13 20:35pm
v4

This is not C# and cannot compile as C#. You should not be so inaccurate if you want help.

—SA
 
Share this answer
 
Comments
patil.nitin8358 18-Jul-13 2:41am    
if u know it then only u can answer it. for ur information c & C# logic is same dont go on language just answer if u know
Sergey Alexandrovich Kryukov 18-Jul-13 3:02am    
What you say makes no sense. "Same logic" is simply not true. You are asking the question where the difference is essential. "For your information"...
—SA
Sergey Alexandrovich Kryukov 18-Jul-13 3:12am    
Look at the operator precedence, evaluation of || and see if ++k has to be evaluated or not. If not, it won't be incremented.
—SA
H.Brydon 18-Jul-13 3:26am    
+5 for enduring the pain of the downvote. :-)
Sergey Alexandrovich Kryukov 18-Jul-13 10:34am    
Thank you, Harvey. By the way, there is no a downvote at the moment...
—SA
Refer to this link

How function works[^]
 
Share this answer
 
Comments
patil.nitin8358 18-Jul-13 2:48am    
if u know it then only u can answer it. for ur information dont go on language just answer if u know the answer reply it
Thanks7872 18-Jul-13 2:54am    
Have you gone through that link? Its an answer to your question. We are not here to do homework for you.
Sergey Alexandrovich Kryukov 18-Jul-13 3:04am    
My 5.
—SA
Thanks7872 18-Jul-13 3:06am    
It really hurts me when i find that such a wonderful place is being spammed through questions like this. Its very sad for me.
Sergey Alexandrovich Kryukov 18-Jul-13 3:17am    
Well, this question is by far not the worse, if we compare it with others these days, but I'm also concerned with the quality of questions and many answers which goes down. After all, reputation of the resource suffers. After all, people who really deserve help and can use it may go away...
—SA
Hi,
Your answer is in your question..:-)

The use of operators..
use of logical operator the first && operators is works due to preceding high priority so i and j increment with 1 and due to || operator it works only for j not for k .. you can swap j and k then you can get j is not increment but k will increment.

Due to || operator any one condition is true, It works...

m=++i&& ++j||++k;


:-)
 
Share this answer
 
v2
Comments
patil.nitin8358 18-Jul-13 3:19am    
so i need to check priority of logical operator
Hemant Singh Rautela 18-Jul-13 3:33am    
Its all not on base priority its base on || operator...
Google should always be your first port of call. It can save everyone much angst.

http://stackoverflow.com/questions/16271779/why-isnt-k-incremented-in-the-statement-m-i-j-k-when-i[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900