Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which of the following combinations of post & pre-increment operators have undefined behaviour in C?

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:

  1. 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?

  2. 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.

like image 927
claws Avatar asked Nov 28 '12 22:11

claws


People also ask

Which of the following combination is LCR?

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.

Which of the following combinations of day lecture is correct?

Right Answer is: A Days – Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.

Which of the following combinations will not result in the formation of a buffer solution?

Combination of NaOH and CH3COOH is the mixture of alkali and acetic acid. Therefore this combination can not be buffer forming solution.

Which of the following combination in an aqueous medium will give a red color or precipitate?

The following combinations in an aqueous medium will give a red colour or precipitate: Fe3++SCN−


2 Answers

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.

like image 84
P.P Avatar answered Oct 20 '22 03:10

P.P


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.

like image 39
Keith Thompson Avatar answered Oct 20 '22 04:10

Keith Thompson