446,192 Members | 811 Online
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

# some1 tell me wats goin on....

 P: 3 Hi there...im new to C programmin and ive gotten a little stuck with precedence of C operators...it wud be great if some1 cud tell me how the compiler goes about solving these kinds of expressions.... i=0; 7||0&&++i; why doesnt the value of i change??? Feb 18 '07 #1
4 Replies

 Expert 100+ P: 1,510 Hi there...im new to C programmin and ive gotten a little stuck with precedence of C operators...it wud be great if some1 cud tell me how the compiler goes about solving these kinds of expressions.... i=0; 7||0&&++i; why doesnt the value of i change??? && (AND) has a higher precedence than || (OR) so the expression Expand|Select|Wrap|Line Numbers 7||0&&++i;    is evaluated Expand|Select|Wrap|Line Numbers 7||(0&&++i);  remember unlike arithmetic operators C guarantees that the operands of binary logical operators are evaluated from left to right (after any precedence) so in 0&&++i the 0 (false) will be evaluated first. In addition, if the final result of the expression can be determined from the value of first operand (in this case false) the second operand (int this case ++i) is not evaluated. Thus the value of i is not changed because the ++i is never evaluated because 0 the first operand of && is false Feb 18 '07 #2

 P: 3 && (AND) has a higher precedence than || (OR) so the expression Expand|Select|Wrap|Line Numbers 7||0&&++i;    is evaluated Expand|Select|Wrap|Line Numbers 7||(0&&++i);  remember unlike arithmetic operators C guarantees that the operands of binary logical operators are evaluated from left to right (after any precedence) so in 0&&++i the 0 (false) will be evaluated first. In addition, if the final result of the expression can be determined from the value of first operand (in this case false) the second operand (int this case ++i) is not evaluated. Thus the value of i is not changed because the ++i is never evaluated because 0 the first operand of && is false Hi horace!! I get what you said and in fact i came up with this explanation myself initially....what got me really confused is this theory doesnt seem to hold for the following code; its pretty much the same code, with the zero changed to a 1: int i=0; 7||1&&++i; of what i gathered from your post, the compiler goes about this expression in the following way : && has higher precedence compared to ||, so we have 7||(1&&++i). while tryin to evaluate 1&&++i (i reckon theres is no short circuit evaluation that comes into the picture here) the value of i should have changed. However its value still remains 0!!This is precisely what is confusing me..... Feb 18 '07 #3

 Expert 100+ P: 1,510 Hi horace!! I get what you said and in fact i came up with this explanation myself initially....what got me really confused is this theory doesnt seem to hold for the following code; its pretty much the same code, with the zero changed to a 1: int i=0; 7||1&&++i; of what i gathered from your post, the compiler goes about this expression in the following way : && has higher precedence compared to ||, so we have 7||(1&&++i). while tryin to evaluate 1&&++i (i reckon theres is no short circuit evaluation that comes into the picture here) the value of i should have changed. However its value still remains 0!!This is precisely what is confusing me..... thinking about it again we rememebr C guarantees that the operands of binary logical operators are evaluated from left to right. so Expand|Select|Wrap|Line Numbers 7||(0&&++i); 7||(1&&++i).   in both cases the || is evaluated and it true so (1&&++i) or (0&&++i) is never eveluated, so i = 0 now consider Expand|Select|Wrap|Line Numbers 0||1&&++i;   0 is false so 1&&++i is evaluated 1 is true so ++i is evaluated (i is incremented to 1) and the overall expression is true now Expand|Select|Wrap|Line Numbers 0||0&&++i; 0 is false so 0&&++i is evaluated 0 is false so ++i is not evaluate the overall expression is false do you rekon we are there now? Feb 18 '07 #4

 P: 3 thinking about it again we rememebr C guarantees that the operands of binary logical operators are evaluated from left to right. so Expand|Select|Wrap|Line Numbers 7||(0&&++i); 7||(1&&++i).   in both cases the || is evaluated and it true so (1&&++i) or (0&&++i) is never eveluated, so i = 0 now consider Expand|Select|Wrap|Line Numbers 0||1&&++i;   0 is false so 1&&++i is evaluated 1 is true so ++i is evaluated (i is incremented to 1) and the overall expression is true now Expand|Select|Wrap|Line Numbers 0||0&&++i; 0 is false so 0&&++i is evaluated 0 is false so ++i is not evaluate the overall expression is false do you rekon we are there now? oh right.......got it now....thanks a lot..... Feb 18 '07 #5