3

In many of the C expressions, white spaces are ignored (example: in case of a**b, where b is a pointer, whitespace is ignored) . But in few cases they cannot be ignored. We get many SO posts on x+++y and related (c++ spaces in operators , what are the rules). I know x+++y really mean (x++) + Y because of higher precedence for postfix. Also there is a difference between x++ +y and x+ ++y. So whitespaces are not always ignored in c expressions. I want to know what is the rule for whitespaces in expressions. Where it is defined? When they are not ignored? Is it when two operators come one after the other especially increment/decrement operators?

Rajesh
  • 1,085
  • 1
  • 12
  • 25
  • I believe your reference is [**C11 Standard (draft n1570) §5.1.1.2 Translation Phases**](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) See (3) and (7) specifically. – David C. Rankin Aug 05 '17 at 18:17
  • The catchy term for the behaviour is ['maximal munch rule'](https://en.wikipedia.org/wiki/Maximal_munch). Note that recent versions of C++ have modified the maximal munch rule for nested template notation `type1>` — under maximal munch, the `>>` is an error (there should be a space between the two closing `>` symbols. – Jonathan Leffler Aug 05 '17 at 20:54
  • The duplicate is the earliest question I found (after a casual search) which discusses 'maximal munch'. The question has been closed as a duplicate of two others, but those others are about the meaning of long sequences of increment operators, not so much about how it is parsed to get to a sequence of operators. You can ignore those duplicates. There are probably a number of other questions on SO about the topic that could also be referenced. An SO search term '[c] maximal munch' throws up 30+ questions. – Jonathan Leffler Aug 05 '17 at 21:01

1 Answers1

6

Whitespace is only relevant for creating tokens. + and ++ are both valid tokens. The rule in C is that a token is formed from the longest sequence of characters that would create a valid token, so "++" without whitespace becomes a single "++" token while "+ +" with a space character becomes two "+" tokens. Since there is no "+++" token, "+++" becomes a "++" token followed by a "+" token.

gnasher729
  • 51,477
  • 5
  • 75
  • 98