473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Clarify the associativity

Expand|Select|Wrap|Line Numbers
  1. int i=-3,j=2,k=0,m;
  2.   m=++i||++j&&++k;
  3.   printf("\ni=%d""\nj=%d""\nk=%d""\nm=%d",i,j,k,m);
  4.  

FOR THE ABOVE CODE my compiler gives the answer as
i=-2, j=2, k= 0, m=1

At first ++i||++j is evaluated. For the || operator if one operand is non-zero the result is non-zero.hence here i is increased to -2 and ++j is not evaluated...

But after this -2 should be ANDed with k. Since for the && operation both the operands should be evaluated to find the result according to me K should be increased to 1. But here the K is not increased. Kindly clarify the reason pls
Aug 27 '10 #1

✓ answered by newb16

Parentheses make no difference as && has higher precedence than || , so they evaluate left to right, evaluating the left argument of || first, then (is if was 0) its right argument, which is ++j && ++k , not ++j alone.

4 1035
newb16
687 512MB
No. With parentheses, the expression is
m=++i||(++j&&++k);
First, ++i is evaluated. As it is nonzero, the result of || operation is true (1) and the rest is not evaluated at all (short-circuit evaluation). i gets incremented to -2 and the other are not changed.
Aug 27 '10 #2
Thanks for your reply. But here the expression will be evaluated from left to right(imagine the condition without paranthesis) and hence I doubt the AND operation should take place.Kindly clarify
Aug 27 '10 #3
newb16
687 512MB
Parentheses make no difference as && has higher precedence than || , so they evaluate left to right, evaluating the left argument of || first, then (is if was 0) its right argument, which is ++j && ++k , not ++j alone.
Aug 27 '10 #4
Thanks newb16.......i have understood it clearly....
Aug 29 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mantorok Redgormor | last post by:
Which section(s) in the standard describe associativity rules? e.g., *ptr++ * and ++ are the same precedence wise and as such will be evaluated left-to-right? --
4
by: Chad | last post by:
The following question stems from p.132 in the book "The C Programming Language", second edition by K & R. The have struct { int len; char *str; } *p;
5
by: junky_fellow | last post by:
Hi, I have a very basic doubt about associativity and precedence of operators. I am not a computer science person and may find it quite weird. Consider an expression 4+5*2
2
by: bochengnever | last post by:
( ) and -are left to right in the same order . eg: struct foo { int a ; void * p; } main() { struct foo* A= malloc(sizeof(struct foo));
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
5
by: fdmfdmfdm | last post by:
Associativity in C takes two forms: left to right and right to left. I think the K&R book lacks something... For example, *p++, since the associativity is from right to left, do this expression...
8
by: subramanian100in | last post by:
What does "associativity of operators" mean ? I am unable to undersatand this from K & R 2nd edition. Kindly explain with an example. Thanks
31
by: Jim Langston | last post by:
In Python 2.5 on intel, the statement 2**2**2**2**2 evaluates to 20035299304068464649790723515602557504478254755697514192650169737108940595563114...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.