3 ms·
The standard itself has a better example (well, older versions). In C99 it's: printf("Eh???/n"); Which is the same as: printf("Eh?\n"); I.e. the "??
by avar 4y ago
The standard itself has a better example (well, older versions). In C99 it's:
printf("Eh???/n");
Which is the same as:
printf("Eh?\n");
I.e. the "??/" supplies the "\" to the "n", making it a "\n". I think it's good that they're gone (they were there to support some ancient non-ASCII systems). But it's worth mentioning that the "horrifying" semantics are there so that these trigraphs combine with <s>digraphs</s> (edit: "escape sequences", see downthread) in this way.
Otherwise you'd just get:
printf("Eh?\\n");
- GrumpySloth 4y agoThis example doesn’t involve digraphs. Trigraphs also preceded digraphs, so their semantics couldn’t have been motivated by digraphs. In fact digraphs were added as a better-thought-out alternative to trigraphs. Digraphs were added in C99, whereas trigraphs were added in C89.
- avar 4y agoSorry, you're completely right. I had digraphs confused with character-constant escape sequences (as defined in "6.4.4.4" in C99). I.e. that the trigraphs need to be parsed like that both because they're replacements for basic syntax like "{" and "}", but also because they need to be considered as if though the parser saw a "\" in a constant, as they next character may be e.g. "n", forming a "\n", not "\\n".