i found this problem in c/c++
when i wrote this code
int a=5,b;
b=++a +(+=a + a--);
it gave the output of b as 21
and when i wrote
int a;
int b=++a + (++a + a--);
it gave b as 18
why the answers are varying for the same compiler(turbo c++)?
also when i tried this code in gcc compiler
the output was 20...
topsyturby
0
Newbie Poster
Recommended Answers
Jump to PostWhen you modify a variable more than once between sequence points, any result is possible. You have invoked
Jump to PostThe result is 100% unpredictable when you invoke undefined behavior. Trying to reason why the output is one way or another is wasted effort.
Jump to Posti m not getting this problem in languages like java.
Languages like Java force expressions like this to work they way the designers wanted. This is easier to manage when the compiler only has to worry about running in a virtual machine rather than any possible combination of hardware on …
All 9 Replies
Tom Gunn
1,164
Practically a Master Poster
topsyturby
0
Newbie Poster
Tom Gunn
1,164
Practically a Master Poster
topsyturby
0
Newbie Poster
Tom Gunn
1,164
Practically a Master Poster
topsyturby
0
Newbie Poster
Tom Gunn
1,164
Practically a Master Poster
topsyturby
0
Newbie Poster
Tom Gunn
1,164
Practically a Master Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.