3 ms·
> Generally, it is often assumed that in C spaces don’t contribute much to the interpretation of programming text I can think of only one exception. In functio
by jxy 3y ago
> Generally, it is often assumed that in C spaces don’t contribute much to the interpretation of programming text
I can think of only one exception. In function-like macro definitions, the opening parenthesis `(` must directly follow the identifier. Though I guess the newline is significant in macro definitions in general, too.
Are there other places where white space matters?
- deleted 3y ago[deleted]
- tsimionescu 3y agoYes, in quite a few places. Most pervasively, between any two letters and numbers. `unsignedint` is different from `unsigned int`, as is `inta` from `int a`. Similarly, `1 1` is not the same as `11`, `0x 1` is not `0x1`, and `0. 0` is not `0.0`. `&&`, `||`, `<<`, `>>`, `++`, `--`, `//`, `/*`, `*/`, and all the `sign=` operators also don't allow whitespace. New lines are significant for single-line comments (and macro definitions as you said). Spaces are significant in escape sequences as well - "\n" is different from "\ n". Also related to string or char literals, newlines are also not allowed at all. Spaces are also significant for an obscure feature which was officially removed in C23: digraphs and trigraphs. Before C23, sequences like `??/` (but not `? ? /`) were alternative ways of spelling many other characters. For example, this was a valid program: int main() ??< return 0; %> This also could be detected, as `??/` represents the `\` character, which, if it appears at the end of a single line comment, makes the next line part of the comment. So, the following function will tell you at runtime if the compiler supported trigraphs: int supportsTrigraphs() { // detector ??/ return 0; return 1; }
- lifthrasiir 3y agoThere are some more examples: • In the stringification via `#`, whitespaces are significant but any run will be converted to a single canonical space character. • Macros can only be redefined when conflicting definitions are identical, where all whitespace separations are considered identical and trailing whitespaces are ignored. So you can put multiple copies of `#define FOO 3 + 4` with differing number of whitespaces, but `#define FOO 3+4` is not considered identical. > Similarly, `1 1` is not the same as `11`, `0x 1` is not `0x1`, and `0. 0` is not `0.0`. There is some interesting surprise here because the tokenization specifically looks for the preprocessing number (the `pp-number` non-terminal), which is a superset of both integers and floating points. There exist a set of preprocessing-only numerals, like `0xe+4`, which would be a syntax error once got past the preprocessor. > Spaces are significant in escape sequences as well - "\n" is different from "\ n". This is most visible when the space is between `\` and a newline. GCC and Clang accepts this as an extension but will issue the "backslash and newline separated by space" warning. > Before C23, sequences like `??/` (but not `? ? /`) were alternative ways of spelling many other characters. Note that digraphs (`%>` here) are still allowed. Trigraphs were problematic as they were very early textual replacements (even earlier than the tokenization!), while digraphs are just separate tokens with the same semantics.