Ask your own question, for FREE!
Computer Science 6 Online
OpenStudy (anonymous):

help in increment and decrement operators in c language?? x = 4; y = (++x) + (++x) + (++x); the value of y is 21 . how ?? i am really messed up in pre and post increment and decrement operators ?? pls help

OpenStudy (anonymous):

I think It can't be 21 my answer is 18 x=4; y = (++x) // Here y = 5 , x = 5; The second ++x : // Here y will be 11 (5 + 6) , x = 6 The third ++x // Here Y is 18 (5 + 6 + 7) , x = 7

OpenStudy (anonymous):

answer is not a 21 or 18 .it will be 19 ...just check it

OpenStudy (divanshu):

the answer is 21 only , this is so because the algorithm used to read the expression works on stack

OpenStudy (divanshu):

let me tell you hw this thing works ;; compiler starts to read the expression from right to left, as this is pre increment, it increases the value first and then assigns it, so after first encounter with ++x the value becomes 5 , than 6 on second encounter and 7 on 3 rd encounter so as we see from our eyes , its like y = 7 + 6 + 5 ; but the compiler see it as y = x + x + x ; where x =7 ( remember the precedence of ++ is greater than any other operator ) so it evaluates it to y = 21

OpenStudy (anonymous):

It's undefined behavior. Nobody's right.

OpenStudy (anonymous):

Best way to do it will get you the correct result (21) x = 4; ++x; ++x; ++x; y = x + x + x; instead of those 2 undefined operations (++x) + (++x) in your question. If your teacher is making you write it like that, scold him for programming malpractice :-D

OpenStudy (anonymous):

http://en.wikipedia.org/wiki/Sequence_point

OpenStudy (anonymous):

gud 1

OpenStudy (divanshu):

the answer is 21 and my explanation is totally correct, and this is nt a bad programming practice, it is one of the most common question asked in interviews of software giants like microsoft, infosays etc

OpenStudy (anonymous):

@divanshu I'm assuming that the operation is performed using the C language. Nothing personal, but with your 8 years of experience you should know that operations such as\[\small{\text{++a } + \text{ ++a}}\] are actually undefined by the C standard, so you would actually be what wali ahad got (19) when using gcc and maybe 21 using some microsoft compiler. Perhaps sharp interviewees would point that out during the interview and get the job.

OpenStudy (anonymous):

Maybe in Java or C++, it works exactly as intended.

OpenStudy (anonymous):

http://en.wikipedia.org/wiki/Undefined_behavior

OpenStudy (divanshu):

@ agdgdgdgwngo i agree with u , but u can see that aporvearyan is using c langauge as mentioned in the question . ++x means increase the x by 1 and store it in x

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!