-6

According to the official description of the C language, what number will be returned?

int a, b;
a = 5;
b = a+++++a;
return b;
Robert Martin
  • 16,759
  • 15
  • 61
  • 87
  • 4
    @Tom: Actually, everyone with a bit of knowledge about lexical analysis (nothing C specific needed here) can tell you how it's parsed, and everyone who reads SO regularily should have seen explanations that it's undefined behaviour once a month. –  Sep 15 '11 at 13:19
  • 1
    There are many duplicates of this question already - finding them is difficult though because it seems you can't search for "+++++" or "a+++++a". – Paul R Sep 15 '11 at 13:31
  • @delnan: Make that once a week or more. Some times of the year it becomes daily... – R.. GitHub STOP HELPING ICE Sep 15 '11 at 13:48
  • possible duplicate of [Why doesn't c = a+++++b work in C?](http://stackoverflow.com/questions/5677271/why-doesnt-c-ab-work-in-c) – sidyll Sep 15 '11 at 13:56

1 Answers1

5

It is parsed as:

b = (a++)++ + a;

This is an invalid expression. The increment operator can't be applied twice as (a++) isn't an lvalue.

The tokenizer isn't context-aware and will match the longest token possible, so it is not parsed as the syntactically valid a++ + ++a. (That still would be invalid code, though, since it modifies a twice without a sequence point which invokes undefined behavior.)

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Why would a++ + ++a be invalid? Couldn't it evaluate left-to-right like: (5) + (6) – Robert Martin Sep 15 '11 at 13:24
  • By invalid code @John means that it invokes undefined behaviour. So it would compile, we just don't know what it would do. – V.S. Sep 15 '11 at 13:27
  • 1
    @VolatileStorm: "undefined behavior" could mean "not compile". – Dietrich Epp Sep 15 '11 at 13:30
  • Thanks, I hadn't thought of that before. It's undefined compiler behaviour, which could lead to undefined run-time behaviour, right? But obviously it doesn't have to lead to any run-time behaviour if it doesn't compile! I'd always just thought of it as run-time behaviour :). – V.S. Sep 15 '11 at 13:34
  • @Robert Martin: It's invalid because the language standard explicitly calls it out as undefined behavior (6.5, para 2); an object may not have its value modified more than once by the evaluation of an expression between any two sequence points. Secondly, the order of evaluation for arithmetic expressions is unspecified; the compiler doesn't have to evaluate it left-to-right. – John Bode Sep 15 '11 at 13:47
  • 2
    @Dietrich: The compiler is required to accept a program containing `a++ + ++a` because it won't necessarily be executed. For a worst-case example, consider `if (problem_that_is_not_known_to_halt()) `a++ + ++a`; which only invokes UB when a new result in CS is proved. :-) – R.. GitHub STOP HELPING ICE Sep 15 '11 at 13:51
  • 1
    @R..: I said "could mean", but that doesn't mean it wouldn't compile in all cases. For example, a complier *could* reject `int main() { int i = 4; return i++ + ++i; }` – Dietrich Epp Sep 15 '11 at 14:01