0

For x=7, y=x++ + x++ + ++x is 25 why it is not 24??

Bcz we knw that it's associativity will be like right to left so it will look like 8+8+8= 24 then why it is 9+8+8=25?!

10th Feb 2020, 7:47 AM
Hit Shiroya
Hit Shiroya - avatar
23 Réponses
+ 3
You're welcome Hit Shiroya bro 👌
10th Feb 2020, 8:45 AM
Ipang
+ 9
Hit Shiroya Here should be 7+8+10 = 25 because x++ = 7 next x++ = 8 because x incremented by 1 after first x++ ++x = 10 because x was 9 after second x++ so here ++x would be ++9 = 10
10th Feb 2020, 8:04 AM
A͢J
A͢J - avatar
+ 4
Add 7 X is 8 Add 8 X is 9 X is 10 Add 10 7+8+10=25
10th Feb 2020, 7:52 AM
Oma Falk
Oma Falk - avatar
+ 2
Hello Hit Shiroya , Understand that even though the code runs in Code Playground, you would see that there's a warning. Mainly telling you that operation on <x> may be undefined. There is no definitive correct result for the expression evaluation should we ever get such a warning. Because output will most likely depends on compiler policy for expression evaluation. What you see in one compiler *may not* be the same with that of another. The general rule in C or C++ would be to avoid, at best, any attempt of modifying while accessing a data more than once in a single execution point. Violation to this may result in implementation-defined outcome. Other languages, such as Java or C#, doesn't suffer from this case because they use the one compiler which employs uniformed policy on how expressions are to be evaluated. P.S. I'd say your professor may need to learn better ways to educate the students 😁 (Just kidding) https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points
10th Feb 2020, 8:39 AM
Ipang
+ 2
Lakhan Rajput Why don't you make a thread? And what to explain? Assume that I understand what you're asking, Character (talking about char, not string) and integer is connected, why? Because each character have its own ascii number which is integer (not only alphanumeric), so in your case, if you print x with format %c, it will print A, if you print with format %d (even though its char data type), it will print 65. And y-x is 122-65 which is 57, when you print it as %c, it will print out 9 (the ascii of '9' is 57), and if you print it as %d, it will print out 57. Hope you understand what I say because I'm not good at explaining things.
12th Feb 2020, 3:53 AM
LastSecond959
LastSecond959 - avatar
+ 2
Anmol Kumar Its not ++x first , right to left means you do x++ first which is still 7 at that moment and when the next x++ goes, the value of x is 8. I'm not saying that you're wrong but just saying that as far as I know, when its x++ you do left to right which is x first, if its ++x, you do right to left which is x first too. So it looks like, y=(x++) + (x++) + (++x); y=((7)+1)+((7)+1)+(1+(8)); Step by step, y=(7) + (1+7) + (1+(1+8));
12th Feb 2020, 4:07 AM
LastSecond959
LastSecond959 - avatar
+ 1
It's your question or answer or question answer?
10th Feb 2020, 7:51 AM
A͢J
A͢J - avatar
10th Feb 2020, 8:06 AM
Hit Shiroya
Hit Shiroya - avatar
+ 1
Ipang thanks a lot,i got perfect answer from u..
10th Feb 2020, 8:43 AM
Hit Shiroya
Hit Shiroya - avatar
+ 1
It is true that different compiler may show different output. But try something like this and you will get 25. #include <stdio.h> int main () { int a=7; int b=a++; //a still 7 printf ("b: %d\n", b); int c=a++; //a is 8 printf ("c: %d\n", c); //in this process, a is 9 (post increment) int d=++a; //a is 10 (pre increment to 9) printf ("d: %d\n", d); int e=b+c+d; //7+8+10=25 printf ("e: %d", e); return 0; }
10th Feb 2020, 9:26 PM
LastSecond959
LastSecond959 - avatar
+ 1
x++ means assign value to x first then add 1 and ++x neans add 1 first and then assign value to it thats why y=x++(assign 7 to x then increment 1 in it during operation. So x =8 there) ++x(increment value by 1 of x then assign value to x which is 8) 7+ x++ + ++x =7+8+ ++x =7+8+10 =25
11th Feb 2020, 7:40 PM
mayank verma
+ 1
Lakhan Rajput x data type is char and assign value is 65 . So x contain 65 as integer and 65 ASCII value/char value of 'A',so A show as char value . y data type is integer so it its contain 122 as integer right there 122 ASCII value of 'z' so z show as char value of 122. y-x contain integer value is 122-65 =57 and char value/ ASCII value of 9 is 57 so 9 show in it char value. I hope this will help you to understand it.
12th Feb 2020, 4:10 AM
mayank verma
+ 1
answer is 7+8+10=25
13th Feb 2020, 5:08 AM
Jagbir singh
Jagbir singh - avatar
0
I want to knw which ans is right 24 or 25 i am with 24 but our proffesor is with 25
10th Feb 2020, 7:52 AM
Hit Shiroya
Hit Shiroya - avatar
0
Bcz solo learn compiler shows ans 25
10th Feb 2020, 7:53 AM
Hit Shiroya
Hit Shiroya - avatar
0
I think it would be like 9+8+8= 25??!Oma Falk
10th Feb 2020, 7:57 AM
Hit Shiroya
Hit Shiroya - avatar
0
Its like this : y = 7 + 8 + 10 = 25; Code work like this: First x++ mean that will use x=7 then do the increment x wil be x= 8 then: Y = 7 + x++ + ++x. Second x++ mean x from previous increment x= 8, you wil use x = 8 then do the increment x wil be x = 9 then : Y = 7 + 8 + ++x. Third ++x mean that do the increment for x first, befor use it, from previous increment x=9 when you do the increment x wil be 10 then use it as 10 : Y = 7 + 8 + 10 = 25. (Sorry fo my bad eng)
11th Feb 2020, 12:53 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
0
Coz, in post increment all the operations are done first and incrementation done next... Where as in pre increment all the operations are done next and the incrementation done first
12th Feb 2020, 1:35 AM
sarada lakshmi
sarada lakshmi - avatar
0
int main () { char x=65; int y=122; printf("%d %c %d %c %d %c", x, x, y, y, y-x, y-x); } Explain me?
12th Feb 2020, 3:26 AM
Lakhan lal
0
Becoz for last one i.e ++x ,increment operation is post executed so then we wil get 9+8+8=25. Aftr it vl increment
12th Feb 2020, 6:12 AM
mugada gowtham
mugada gowtham - avatar