I am a novice programmer and need help.....
#include <stdio.h>
int main (void)
}
int n1=2;
int n2=0;
n2= (++n1) + (++n1);
printf("Result= %d",n2);
return 0;
}
Result= 8 How is this possible??
Similarly,
#include <stdio.h>
int main (void)
}
int n1=2;
int n2=0;
n2= (++n1) + (++n1) + (++n1);
printf"Result= %d",n2);
return 0;
}
Result= 13 How is it possible??
All the results does not match up with my understanding of C. Please, I need help.
Thank you.
p.user01 0 Newbie Poster
Recommended Answers
Jump to PostI have ran this program on my compiler DEV C++.
I also get same result=8
I have compiled many times...and It shows same output...The problem with undefined behavior is that it's 100% unpredictable. The results could be intuitive or not. The results could be consistent or not. It could …
Jump to PostI have been following a book named "Let Us C" by Yashavant Kanetkar.
I haven't heard good things about that particular book.
I can't find out the topic "Sequence Point Rule" in here.
That's because your book is for beginners and sequence points are a concept that isn't strictly necessary …
Jump to PostDon't panic...i am back to help you all...
n2=(++n1)+(++n1)
See the concept is that ++n1 is preincrement operator having greater priority so it will first increment n1 value and making it 3.....
n2=(++n1)+(++n1)
++n1 is preincrement operator so it will be incremented to 4....
Now the above code is similar …
Jump to PostYou see certain syntactical expressions have a higher level of precedence than others By this I mean certain codes are "more important and so are executed first...for example ++ has a higher level of precedence that just + and brackets,namely () have an even higher order of precedence than ++ …
Jump to PostBetter you must read it properly....
Better you reread the thread because you don't seem to understand the clear explanation that Narue was giving about undefined behavior.
All 31 Replies
Narue 5,707 Bad Cop Team Colleague
Utsav Chokshi 0 Newbie Poster
Narue 5,707 Bad Cop Team Colleague
p.user01 0 Newbie Poster
Narue 5,707 Bad Cop Team Colleague
p.user01 0 Newbie Poster
p.user01 0 Newbie Poster
Narue 5,707 Bad Cop Team Colleague
chrjs 42 Junior Poster in Training
Narue 5,707 Bad Cop Team Colleague
p.user01 0 Newbie Poster
Narue 5,707 Bad Cop Team Colleague
sbesch 2 Newbie Poster
p.user01 0 Newbie Poster
Narue 5,707 Bad Cop Team Colleague
tomato.pgn -6 Posting Whiz in Training
tomato.pgn -6 Posting Whiz in Training
Narue 5,707 Bad Cop Team Colleague
CoilFyzx 0 Newbie Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
tomato.pgn -6 Posting Whiz in Training
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
Killer.bee commented: Ohh i think you need to rereas -1
Nick Evan commented: counter. +16
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
tomato.pgn -6 Posting Whiz in Training
Fbody commented: Nice, arguing about pre- vs. post- and both examples are pre- -3
Narue 5,707 Bad Cop Team Colleague
Killer.bee commented: Explain it with explanation where it gave arbitrary result to proof your words -1
jonsca commented: Unwarranted and undeserved +14
tomato.pgn -6 Posting Whiz in Training
Narue 5,707 Bad Cop Team Colleague
p.user01 0 Newbie Poster
tomato.pgn -6 Posting Whiz in Training
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.