Cana anyone explain me this logic? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 7

Cana anyone explain me this logic?

#include <stdio.h> int main() { int x,y,z; x=y=z=-1; z = ++x && ++y || ++z; printf ("x = %d, y = %d, z=%d",x,y,z); return 0; }

9th Aug 2021, 6:16 AM
Ankur Hazarika
16 Réponses
+ 5
If anyone wants to intentionally use the short-circuit evaluation then it is a good idea to make sure the LHS is not to be modified anywhere within an expression. The thing here is, LHS <z> has increment ++ operator applied, while it is also involved as part of the expression. This means, evaluation risks undesired result from sequence point issue. Had it been like so ... z = ++x && ++y || z; There should be less problem, as the LHS <z> is not being modified anywhere in the expression. I think it's even better if all <x>, <y> and <z> do not get modified within the expression. And still, compiler might ask us to clarify the expression by adding parentheses. Just how I see it ...
9th Aug 2021, 2:28 PM
Ipang
+ 4
Calvin, Do so? short-circuit? sequence point? or something else?
9th Aug 2021, 2:40 PM
Ipang
+ 3
Calvin, It's a logic the compiler vendor implemented, which allows the compiler to foresee possible issues as it processes the code I guess ...
10th Aug 2021, 4:23 AM
Ipang
+ 1
rudolph flash, Ofcourse ++ and -- post as well as pre increment or decrement has higher precedence. Here case is different logical expression ++x pre-increment gets evaluated to 0 from -1, as logical expression x evaluate as false because of 0. And && rejects the further calculation and false expression returns 0 value and assign it to 0. DHANANJAY PATEL
9th Aug 2021, 8:38 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
+ 1
rudolph flash, Code in this case is not about precedence of post/pre increment. It is about optimization. If && operator is used for logical expression, FRIST logical expression from one of the two logical expression is false compiler takes decision that logical expression is false because of following reason. FALSE && FALSE => FALSE FALSE && TRUE => FALSE TRUE && FALSE => FALSE TRUE && TRUE => TRUE In above && (AND) logical expression FIRST logical expression evaluated as FALSE it doesn't go for evaluation of SECOND logical expression computation because it has no delimma and it returns result as FALSE and it NEVER try to evaluate SECOND logical expression. And in case of FIRST logical expression is TRUE It needs to evaluate SECOND logical expression to check weather it's TRUE or FALSE to get final answer. Similarly in case of || (OR) logical expression create table and see if FIRST logical expression is TRUE it need not to compute SECOND logical expression because answer is always TRUE DHANANJAY PATEL
9th Aug 2021, 10:06 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
+ 1
Ipang Why does the compiler do so? I get it with logic like '(a = getchar()) && b || c'. I guess that it's some sort of a built-in behaviour to let the user know when the logic can change a variable's value.
9th Aug 2021, 2:34 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Ipang Oh, I mean, the warning which it throws.
9th Aug 2021, 4:29 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Ipang I see, thank you.
10th Aug 2021, 4:31 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
#include <stdio.h> int f(int x) { printf("f = %d\n", x); return x; } int g(int x) { printf("g = %d\n", x); return x; } int h(int x) { printf("h = %d\n", x); return x; } int main() { int x, y, z; x=y=z=-1; z = f(++x) && g(++y) || h(++z); printf("x=%d,y=%d,z=%d\n",x,y,z); return 0; } Above code will help how logical expression gets evaluated. DHANANJAY PATEL
10th Aug 2021, 10:30 PM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
0
Ankur Hazarika Try Googling about 'logical operator short-circuiting' and you'll find the answer to your question.
9th Aug 2021, 7:59 AM
Calvin Thomas
Calvin Thomas - avatar
0
Ankur Hazarika, #include <stdio.h> int main() { int x, y, z; x=y=z=-1; z = ++x && ++y || ++z; printf("x=%d,y=%d,z=%d",x,y,z); return 0; } In case && and || are left associated then it first pre increments x to 0 from -1, 0 value of x falsify expression and assign immediately z with 0. And printf() call outputs x=0,y=-1,z=0 It's called a optimised computation so value of y remains untouched. DHANANJAY PATEL
9th Aug 2021, 8:17 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
0
rudolph flash I don't understand how this could be ambiguous. Could you please elaborate on this matter with a code?
9th Aug 2021, 8:30 AM
Calvin Thomas
Calvin Thomas - avatar
0
rudolph flash, Thanks. I was not aware of sequence point before. I looked into Internet. Case we are discussing is not the same. It is there from beginning. It is the case of compiler behaviour. So one need to be aware of case we are discussing here. for logical AND and logical OR. It makes maintenance person life easy. heard about parameter passing sequence is different in different languages. While linking it needs to be keep in mind the sequence of parameters. Windows takes care about it by using "cdeclare" kind of enhancing function type to be in sync with system library. Other sequence usage is for parallel processing cases while working on multiple processing on single Processor. DHANANJAY PATEL
9th Aug 2021, 11:18 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
0
rudolph flash I guess that short-circuiting is something found in every C compiler, nowadays.
9th Aug 2021, 11:34 AM
Calvin Thomas
Calvin Thomas - avatar
0
Ok. X=y=z=-1 means they assign x,y,z are same and those value is -1 In next one you must think about assign z they used pre incriminating they saying the vay that x and y should change accordingly &&means and ||means .finally they using printf to take the output do Have a nice day ūüėáūüėá
11th Aug 2021, 5:34 AM
W.H.J.Hirushan
W.H.J.Hirushan - avatar
- 1
2233336t643
12th Aug 2021, 11:54 PM
Almazs Hagmj