I've read, Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc...) and tried understanding Sequence points on "comp.lang.c FAQ" after wasting more than 2 hours of time trying to explain the following results by gcc compiler.
expression(i=1;j=2) i j k
k = i++ + j++; 2 3 3
k = i++ + ++j; 2 3 4
k = ++i + j++; 2 3 4
k = ++i + ++j; 2 3 5
k = i++ + i++; 3 2
k = i++ + ++i; 3 4
k = ++i + i++; 3 4
k = ++i + ++i; 3 6
i = i++ + j++; 4 3
i = i++ + ++j; 5 3
i = ++i + j++; 4 3
i = ++i + ++j; 5 3
i = i++ + i++; 4
i = i++ + ++i; 5
i = ++i + i++; 5
i = ++i + ++i; 6
Question:
I want to know if all the expressions shown (in 4 groups) in above figure have undefined behavior? If only some of them have undefined behavior which ones does and which ones doesn't?
For defined behaviour expressions, kindly can you show (not explain) how compiler evaluates them. Just to make sure, if I got this pre-increment & post increment correctly.
Background:
Today, I've attended a campus interview, in which I was asked to explain the results of i++ + ++i
for a given value of i
. After compiling that expression in gcc, I realized that the answer I gave in interview was wrong. I decided not to make such mistake in future and hence, tried to compile all possible combinations of pre and post increment operators and compile them in gcc and then try to explain the results. I struggled for more than 2 hours. I couldn't find single behaviour of evaluation of these expressions. So, I gave up and turned to stackoverflow. After little bit of reading archives, found that there is something like sequence point
and undefined behaviour.
Solution : The LCR circuit used for communicaton should possess high quality factor (Q factor) or resonance, which is given by `Q = (1)/(R ) sqrt((L)/(C ))` <br> To make Q high , R should be low, L should be high and C should be low. Therefore, Choice (c ) is the best suited.
Right Answer is: A Days – Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.
Combination of NaOH and CH3COOH is the mixture of alkali and acetic acid. Therefore this combination can not be buffer forming solution.
The following combinations in an aqueous medium will give a red colour or precipitate: Fe3++SCN−
Except the first group, all expressions in the other three groups have undefined behaviour.
How the defined behviour is evaluated (group 1):
i=1, j=2;
k=i++ + j++; // 1 + 2 = 3
k=i++ + ++j; // 1 + 3 = 4
k=++i + ++j; // 2 + 3 = 5
k=++i + j++; // 2 + 2 = 4
It's fairly straight forward. post-increment vs pre-increment thing.
In group 2 and group 4, it's quite easy to see the undefined behaviours.
Group 2 has undefined behaviour because =
operator doesn't introduce a sequence point.
There are no sequence points within any of these statements. There are sequence points between them.
If you modify the same object twice between consecutive sequence points (in this case, either via =
or via prefix or postfix ++
), the behavior is undefined. So the behavior of the first group of 4 statements is well defined; the behavior of the others is undefined.
If the behavior is defined, then i++
yields the previous value of i
, and as a side effect modifies i
by adding 1
to it. ++i
modifies i
by adding 1
to it, and then yields the modified value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With