6

In the printf statement i+++j, is it always treated as i++ +j?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main() {
    int i =5,j= 6, z;
    z=i^j;
    printf("%d",i+++j);
    return 0;
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
user2713461
  • 387
  • 2
  • 5
  • 16

3 Answers3

19

i+++j is equivalent to i++ + j.

This has nothing to do with operator precedence. The +++ is resolved to ++ + by the compiler before expressions are parsed.

The C standard defines a sequence of translation phases, each using as its input the output of the previous one. +++ is resolved to ++ + in phase 3, which decomposes the source into preprocessor tokens. Operator precedence is not considered until phase 7, syntactic and semantic analysis. (The translation phases don't have to be implemented as distinct phases or passes, but the compiler must behave as if they are.)

The rules that says +++ is resolved to ++ + and not + ++ is what's informally called the "maximal munch rule". It's stated in section 6.4 paragraph 4:

If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token.

(Amusingly, the index refers to "maximal munch", but that term isn't mentioned anywhere else in the standard.)

This also implies that i+++++j, which could be tokenized as the valid expression i++ + ++j, is actually i ++ ++ + j, which is a syntax error.

Of course the solution, for a programmer, is to add whitespace to make the division into tokens clear: i++ + j. (i+++j is perfectly clear to the compiler, but i++ + j is much clearer to a human reader.)

Reference: N1570, section 6.4, paragraph 4. N1570 is a draft of the 2011 ISO C standard. This rule is unchanged from earlier versions of the standard. Translation phases are discussed

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

Yes. It will be parsed as (i++) + (j).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
haccks
  • 104,019
  • 25
  • 176
  • 264
-2

Since postfix increment/decrement operator has higher precedence over the addition operator, there is no doubt it would be treated as (i++) + j. So, it is not a compiler issue, you should rather consider the operator precedence chart.

Also, I would suggest you to put proper spaces between the expressions, it would be beneficial in case you go through your code later on. :)

Hope that helps!

Ashima
  • 129
  • 1
  • 11
  • If it is not compiler then who is responsible for parsing the expressions? – haccks Sep 01 '13 at 17:33
  • 1
    "compiler would not depend on it" - it will. `i++ +j` and `i+ ++j` are parsed differently. –  Sep 01 '13 at 17:39
  • @haccks yes compiler is responsible and it would depend on the spaces, my answer in the question's context. And I have edited it for clarity purpose. – Ashima Sep 01 '13 at 17:57
  • 7
    It has nothing to do with operator precedence. See my answer. – Keith Thompson Sep 01 '13 at 19:17
  • 1
    [Scaner](http://en.wikipedia.org/wiki/Lexical_analysis#Scanner) : This is known as the `maximal munch rule`, or `longest match rule` – Grijesh Chauhan Sep 02 '13 at 04:19
  • @KeithThompson Why it has nothing to do with operator precedence? – Ashima Sep 02 '13 at 14:27
  • 3
    The C standard defines a sequence of *translation phases* each using as its input the output of the previous one. `+++` is resolved to `++ +` in phase 3, which decomposes the source into preprocessor tokens. Operator precedence is not considered until phase 7, syntactic and semantic analysis. (The translation phases don't have to be implemented as distinct phases or passes, but the compiler must behave as if they are.) – Keith Thompson Sep 02 '13 at 17:12