0

This is an ugly code used only to terrorize job applicants during interviews... But I cannot understand the logic behind it. Can someone explain why the expression with "b" is not equal to the one with "a"?

#include <stdio.h>
void main(){
    int a = 1, b = 1, c = 1;
    printf("%d %d %d", ++a + a++ + 1, 1 + ++b + b++, ++c + c++); // displays 6 5 5
}

Thank you very much.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 3
    Is this actually valid? Doesn't it violate the restriction against reading and modifying the same value multiple times between sequence points? – Barmar Apr 18 '14 at 23:28
  • 5
    The only thing that question will tell you about an applicant is that if he/she gets up and walks out of the interview, that was the person you should have hired. – Carey Gregory Apr 18 '14 at 23:31
  • 2
    The code invokes undefined behavior and is (I assume) mostly in the interview to check if the user has the guts to say they don't know (or know it's undefined) – Joachim Isaksson Apr 18 '14 at 23:38

3 Answers3

1

The logic is simple:

Create Undefined Behavior and let the nasal demons terrorise the job applicant. That is known as job security.

If you write to a variable, you must not access it again without intervening sequence point except to calculate the value which shall be written.

There's a second case of UB or at least Implementation Defined Behavior:

void main()

Should be one of

int main(void)
int main(int argc, char* argv[])
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • Did C++11 change those rules? I need to go look that up now. I suspect that operator overloading and native operators got all clarified and stuff. – Zan Lynx Apr 18 '14 at 23:35
  • @ZanLynx: They followed C and moved from sequence points to sequenced before/after/indeterminately/not, though without changing sematics. – Deduplicator Apr 18 '14 at 23:38
  • Something like that. I believe that in all versions of C++ if a user defined class was used instead of int, this would be fully defined, and in C++11 it would be fully defined with int. – Zan Lynx Apr 18 '14 at 23:41
  • @ZanLynx: Lucky thing he never used the same variable for two arguments. – Deduplicator Apr 18 '14 at 23:47
  • @Zan - yes, the sequencing rules change in C++11 and some things that were undefined in C++98 are now well-defined in C++11. – M.M Apr 19 '14 at 01:20
1

Read up on Undefined behavior and sequence points.

This is a slightly different, yet similar example (thanks Zan):

2) Furthermore, the prior value shall be accessed only to determine the value to be stored.

C++ example:

std::printf("%d %d", i,++i); // invokes Undefined Behaviour because of Rule no 2
Community
  • 1
  • 1
JAL
  • 41,701
  • 23
  • 172
  • 300
  • 1
    small difference there in that your example uses `i` in two different function parameters. the question uses a single variable in each parameter. – Zan Lynx Apr 18 '14 at 23:42
  • @ZanLynx Oh whoops, you're right. The thread is a good read regardless. – JAL Apr 18 '14 at 23:43
0

Just to prove that this is a terrible idea, here's the output in VS2012 (technically C++ compiler I suppose):

5 5 4

GCC & G++:

6 5 5

So apparently your interviewer tried it in GCC.

IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17