0
can you make me understand this c code?
int x=1; printf("%d %d %d",x++, x, ++x); i expected o/p: 1 2 3. but the output came 2 3 3 why??
20 odpowiedzi
+ 8
The result may vary depending on the C compiler. There is no defined order of evaluation.
The current C standard ISO/IEC 9899:2018 says:
„If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpression of an expression, the bahavior is undefined if such an unsequenced side effect occurs in any of the orderings.“
(reference: ISO/IEC 9899:2018 - 6.5 Expressions - paragraph 2)
This means, you should not use such code, as its evaluation is undefined.
+ 7
All that I can tell you is just please stay away from such messy mix of modification and access of a single data in one execution point.
What you see is no definite answer. Output depends on compiler implementation. So different compiler gives different output, if I understood it correctly.
You mind to tell where you found it? was it in a challenge?
+ 3
I was curious and tried three different compilers. The results were
GCC: 2 3 3
Clang: 2 2 2
Zapcc: 1 2 3
Clang and Zapcc displayed a warning in addition during compilation: “warning: unsequenced modification and access to ‘x’ [-Wunsequenced]”
+ 2
Ipang This question was asked me in placement interview.
+ 2
Harsh Bhardwaj
I don't know what conclusion can the interviewer make from asking such a question. Hope it doesn't mean they encourage such a mess that they expected their employee-to-be to know the answer.
+ 2
Martin Taylor
Since you mentioned "Even a moderately trained C programmer should know this", I suppose you have the definite answer?
+ 2
If you know Hindi then you can go to you tube and learn concept from " MySirg.com by Saurav sukla sir"
If don't, then some Important website like
1. JAVATPOINT
https://www.javatpoint.com/
2. GREEKFORGREEK
https://www.geeksforgeeks.org/
+ 1
Michael do you want to say that every compiler has different logical computation process?
+ 1
For GCC I showed the algorithm here: https://www.sololearn.com/Discuss/1935920/?ref=app
Starting evaluations from right and pushing only at postincrements:
x++ x ++x
(3) (2) (1)
postincrement plain value preincrement
first push: 2 only eval: 2 only eval: 2
then eval: 3
After pushing '2' at position (3) - due to postincrement - fill remaining pushes at positions (2) and (1) with last evaluated value '3'.
So the result in GCC is 2 3 3
+ 1
We should know first if "printf()" contain more than expressiontwo then it evaluate from right from left.
1. "printf()" behave like a stack data structure
i.e the element or value is inserted at first is
remove at last.
2. ++x is insert first, then x and after x++
3. You should also know pre- increment operator
increment value then assigned in variable
4. post- increment operator assigned value first
in variable then it increment
5. So, ++x value is 2, x value is also 2, and x++ value is also 2
+ 1
Very good you are multi- talented
0
Bhai mujhe tere number chahiye the
0
Krishan Das Gurjar kaise number ?
0
Thanks guys 👍
now i have understood the process of post-pre incrementation.
0
Praphull Kumar could you tell me where can i learn such deep concepts (background working) of C. so that i would correctly predict such expressions without compile them. I want to dig deep in C.
0
Hindi janta hu bhai mai.😊 Thanks
0
😀i don't think so
0
Martin Taylor
My apologies, when you wrote "Even a moderately trained C programmer should know this" I misunderstood the 'this' word as 'the answer to this'. But from the few reads I did, I knew there's just no definite answer of the output. And that's why I needed to confirm again, because I agree with you and Michael, that the output is compiler implementation defined ✌