What happens in the printf statement to print 8 !!!Code:#include<stdio.h> main() { int a=1; printf("%d\n",a+=(a+=3,5,a)); }
What happens in the printf statement to print 8 !!!Code:#include<stdio.h> main() { int a=1; printf("%d\n",a+=(a+=3,5,a)); }
Expressions
Learn about sequence points, and the "modified at most once" rule.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
@Salem...as you said i went through those concepts...and now i get a bit of those....but im not getting how the expression "a+=(a+=3,5,a)" is sequenced for evaluation ??
How many times is a being modified between one ; and the next ; (otherwise known as sequence points).
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I am reallly confused.....im only able to make some guess by trying to modify the code in different ways...each time gettin different answers.....as i dont want to put up here my guesswork and be really specific...would you ease me with 'how' and 'what' happens in the statement "a+=(a+=3,5,a) "
My answer would be to write proper code which doesn't invoke UB and get on with life.
These silly assignments by clueless teachers are not worth the effort (yours or mine).
The simple answer is to run the code through a number of compilers, take what appears to be the most "popular" answer, and submit that.
If your clueless teacher wants to argue over it, then show them ALL your results and then refer them to the comp.lang.c FAQ I posted.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
We get so flooded with these stupid undefined behavior questions. I continue to await one of these Indian students (as that's where these assignments appear to come from) telling us whether they are being taught this as what not to do (because it's undefined behavior) or if they are actually being taught this is valid code. Unfortunately, as a rule, these posters are one-hit wonders.
All write guys thanks anyways for your concern.
@rags....that was an understatement.
xyz88
I had a Computer Exam a few days ago... and some of the code in the paper was so utterly horrible that I just wish I had the gall to draw a big troll (Problem Bro? ) shouting the possible problems with those codes .... in the answer script.