4

I'm trying to understand the answer to an interview question.

The following code is presented:

int x = 5;

int y = x++ * ++x;

What is the value of y?

The answers are presented as a list of multiple choice answers, one of them being 35.

Writing and running this code on my machine results in y being equal to 35.
Therefore I would expect to mark the answer to this question as 35.

However, isn't this expression undefined due to the x++ i.e. the side of effect (the actual incrementation of x) could happen at any time depending on how the compile chooses to compile the code.

Therefore, I would not have thought that you could say for certain that the the answer is 35 as different compilers might produce difference results.

Another possible multiple choice answer is 30, which I would have thought was also viable i.e. if the post increment side effect takes place towards very end of the sequence point.

I don't have the answer key, so it's hard to determine what the would be the best answer to give.

Is this just a poor question or is the answer more obvious?

user3742467
  • 524
  • 3
  • 14
  • 1
    This is classic undefined behaviour, there is no correct answer. – Havenard May 30 '15 at 22:09
  • 3
    If one of the answers *isn't* "undefined behavior", then it's a poor question. – John Bode May 30 '15 at 22:11
  • 1
    The correct answer is "this is crap code - sack the programmer - it would not past the code review" – Ed Heal May 30 '15 at 22:12
  • I may have been misunderstanding something, but I wasn't aware that this was undefined behavior, According to the C spec, there is an order of precendence, and all three of these operators are in diffet levels. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf ( The syntax specifies the precedence of operators in the evaluation of an expression, which is the same as the order of the major subclauses of this subclause, highest precedence first.section 6.5.6 note 72 ) -- Self Edit, I found the notes about sequence points.and understand now – Matthew Carlson May 30 '15 at 22:14
  • My philosophy in life is not to do two or more things at the same time - if you do you do both poorly. – Ed Heal May 30 '15 at 22:16
  • @MatthewCarlson: 6.5/2: "If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined." – John Bode May 30 '15 at 22:18
  • Thanks @JohnBode, I rarely have to dive into the spec, and I learn something new every time, and though I've been professionally writing C89 for quite some time now, I've apparently never had occasion to worry about sequence points, – Matthew Carlson May 30 '15 at 22:20
  • Thanks all for your answers. Glad to know that I’m not going crazy! – user3742467 May 30 '15 at 22:26

3 Answers3

5

You are correct; the Standard does not impose any requirements at all on what this code might do.

If this was a multiple-choice question that required you to choose a specific defined answer, then — whoever wrote the question doesn't understand C as well as you do. Keep that in mind when you decide whether to accept an offer there. :-)

ruakh
  • 175,680
  • 26
  • 273
  • 307
2

Undefined behavior means UNDEFINED, sometimes it could be the EXPECTED behavior, it doesn't mean it's DEFINED.

Perhaps they want you to answer what the expected behavior is, which is none since it's undefined behavior.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0

You are correct that this is undefined. However...

Often these questions are designed to make you think and, even though undefined by standard, the questioner may be looking to see if you:

  1. Know what happens "in the real world" most of the time. (I'm not saying this is good, but I am saying that you will find constructs this bad and worse in codebases that you may be asked to take over and maintain.)
  2. Know about operator precedence and order of operations.
David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
  • 3
    What does happen in the real world most of the time? You make it sound like this is fine as long as you know what the compilers are doing. – Captain Giraffe May 30 '15 at 22:14
  • @CaptainGiraffe That is not my intent at all. I'm pointing out *why* people ask this kind of question, I'm not saying it should ever be used. Let's be honest, you will absolutely find things this bad and worse in existing code bases that you may be asked to maintain. – David Hoelzer May 30 '15 at 22:15
  • @DavidHoelzer that is the point of @[ruakh's answer](http://stackoverflow.com/a/30551879/1983495) _Keep that in mind when you decide whether to accept an offer there. :-)_ – Iharob Al Asimi May 30 '15 at 22:17
  • @iharob Yes, that is an excellent point. I didn't have the benefit of reading it since I was in the middle of writing when you posted. ;) – David Hoelzer May 30 '15 at 22:18
  • @DavidHoelzer I didn't post that @[ruakh](http://stackoverflow.com/users/978917/ruakh) did. – Iharob Al Asimi May 30 '15 at 22:19
  • Re: "Know about operator precedence and order of operations": Note that even aside from the undefined-behavior aspects here, there's no guarantee which operand of `+` is evaluated first. For example, if a function `f` modifies a variable that is used by a function `g`, then `f() + g()` doesn't invoke UB, but it also doesn't specify which function is called first, so there are two different possible results. So even in a compiler that safely handles `x++ * ++x`, the result could just as plausibly be 36 as 35. – ruakh May 30 '15 at 22:24