3

Does spaces have any meaning in these expressions:
assume:

int a = 1;  
int b = 2;  

1)

int c = a++ +b;

Or,
2)

int c = a+ ++b;

When I run these two in visual studio, I get different results. Is that the correct behavior, and what does the spec says?
In general, what should be evaluated first, post-increment or pre-increment?

Edit: I should say that
c =a+++b;
Does not compile on visual studio. But I think it should. The postfix++ seems to be evaluated first.

user3599803
  • 6,435
  • 17
  • 69
  • 130
  • 2
    It is the correct behavior. – Jarod42 Feb 10 '17 at 12:13
  • You can't have a space in the middle of an operator name. Would you expect calling `void foobar();` as `foo bar()` to work? – BoBTFish Feb 10 '17 at 12:14
  • 5
    You should be asking what this does: `int c = a+++b;` – juanchopanza Feb 10 '17 at 12:16
  • In your examples you diferently call addition (+), preffix increment(++x) and postfix increment(x++). Look at [operator precedence](https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence) and [operators overloading](http://en.cppreference.com/w/cpp/language/operators). – Yuriy Ivaskevych Feb 10 '17 at 12:17
  • You should search SO for "sequence points" - you'll find the answer there. – acraig5075 Feb 10 '17 at 12:19
  • I think the order of evaluation between a and b depends on the compiler. In case 1, the original value of a is used to calculate c, only then it is incremented. In case 2, b is first incremented, then its value is used to calculate c. – Milack27 Feb 10 '17 at 12:20
  • @acraig5075 No, not much to do with sequence points. – juanchopanza Feb 10 '17 at 12:20
  • @Milack The order of evaluation doesn't matter at all here. – juanchopanza Feb 10 '17 at 12:21
  • @juanchopanza Correct, just mentioning. – Milack27 Feb 10 '17 at 12:21
  • @juanchopanza Oh, I'm surprised. `++` is always used as the textbook example of not being a sequence point so you can't rely on left-to-right evaluation order with this type of statement. – acraig5075 Feb 10 '17 at 12:22
  • 2
    @acraig5075 Yes but the thing is, sequence points are irrelevant here. – juanchopanza Feb 10 '17 at 12:23
  • 1
    One important rule is that operators can't have empty spaces. Other than that, see http://stackoverflow.com/questions/7485088/what-does-the-operation-c-ab-mean – juanchopanza Feb 10 '17 at 12:27
  • 2
    @BoBTFish : You are clearly not an old FORTRAN programmer. Spaces used to only be relevent inside Hollerith constants and quoted strings. You really could call FOOBAR() as FOO BAR(). – Martin Bonner supports Monica Feb 10 '17 at 13:32

3 Answers3

3

Is that the correct behavior

Yes, it is. Postfix ++ first returns the current value, then increments it. so int c = a++ +b means compute the value of c as the sum between current a(take the current a value, and only after taking it, increment a) and b; Prefix ++ first increments the current value, then returns the value already incremented, so in this case, int c = a+ ++b means compute c as the sum between a and the return of the next expression, ++b, which means b is first incremented, then returned.

In general, what should be evaluated first, post-increment or pre-increment?

In this example, it is not about which gets evaluated first, it is about what each does - postfix first returns the value, then increments it; prefix first increments the value, then returns it.

Hth

3

Maybe it helps to understand the general architecture of how programs are parsed.

In a nutshell, there are two stages to parsing a program (C++ or others): lexer and parser.

The lexer takes the text input and maps it to a sequence of symbols. This is when spaces are handled because they tell where the symbols are. Spaces really matter at some places (like between int and c, to not confuse with the symbol intc) but not others (like between a and ++ because there is no ambiguity to separate them).

The first example:

int c = a++ +b;

gives the following symbols, each on its own row (implementations may do this in slightly different ways of course):

int
c
=
a
++
+
b
;

While in the other case:

int c = a+ ++b;

the symbols are instead:

int
c
=
a
+
++
b
;

The parser then builds a tree (Abstract Syntax Tree, AST) out of the symbols and according to some grammar. In particular, according to the C++ grammar, + as an addition has a lower precedence than the unary ++ operator (regardless of postfix or prefix). This means that the first example is semantically the same as (a++) + b while the second is like a+ (++b).

For your examples, the ASTs will be different, because the spaces already lead to a different output at the lexer phase.

Note that spaces are not required between ++ and +, so a+++b would theoretically be fine, but this is not recommended for readability. So, some spaces are important for technical reasons while others are important for us users to read the code.

Community
  • 1
  • 1
Ghislain Fourny
  • 6,971
  • 1
  • 30
  • 37
  • a+++b does not compile on my visual studio. But does compile on my gcc. It seems like the postfix ++ has a precedence over the prefix++ ? – user3599803 Feb 11 '17 at 07:14
  • Interesting to know that some implementations reject it (maybe for the best). Regarding the behavior when it does compile: this is actually not a matter of precedence (which would happen at the parser phase) but due to the maximal munch rule at the lexer phase: this rule is explained in the corresponding StackOverflow question, linked in the last paragraph above. – Ghislain Fourny Feb 11 '17 at 08:46
2

Yes they should be different; the behaviour is correct.
There are a few possible sources for your confusion.


This question is not about "spaces in operators". You have different operators. If you were to remove the space, you would have a different question. See What is i+++ increment in c++

It's also not about "what should be evaluated first, post-increment or pre-increment". It's about understanding the difference between post-increment and pre-increment.

  • Both increment the variable to which they apply.
  • But the post-increment expression returns the value from before the increment.
  • Whereas the pre-increment expression returns the value after the increment.

I.e.

//Given:
int a = 1;  
int b = 2; 

//Post-increment
int c = a++ +b; =>
        1 + 2; (and a == 2) =>
        3;

//Pre-increment
int c = a+ ++b; =>
        1 + 3; (and b == 3) =>
        4;

Another thing that might be causing confusion. You wrote: a++ +b;. And you may be assuming that +b is the unary + operator. This would be an incorrect assumption because you have both left and right operands making that + a binary additive operator (as in x + y).


Final possible confusion. You may be wondering why:

  • in a++ +b the ++ is a post-increment operator applied to a.
  • whereas in a+ ++b it's a pre-increment operator applied to b.

The reason is that ++ has higher precedence than the binary additive +. And in both cases it would be impossible to apply ++ to +.

Community
  • 1
  • 1
Disillusioned
  • 14,635
  • 3
  • 43
  • 77