5 ms·
> Trigraphs are removed from the language. Finally! My joy knows no bounds.
by GrumpySloth 4y ago
> Trigraphs are removed from the language.
Finally! My joy knows no bounds.
- MaxBarraclough 4y ago> My joy knows no bounds. Much of the time, neither does a C compiler.
- turminal 4y agoWhy? They seem pretty harmless, are they difficult to implement?
- GrumpySloth 4y agoThey’re not difficult to implement. It’s a sed pass running before any preprocessor or even lexer, which is the horrifying thing. They replace characters in source code, they’re not normal tokens like digraphs. And so e.g. the following program won’t print what a mere mortal would expect: #include <stdio.h> int main() { puts(“What??(Really.)”); } Try running that. Only change the quote marks to the ones on a normal keyboard (I’m on my phone.) Edit: Oh, and remember to compile with -std=c11 e.g. gnu11 has trigraphs disabled by default.
- zwirbl 4y agoor put the code into godbolt and take a look at the strings with -std=C11 and without https://godbolt.org/z/zn44rdW6P https://godbolt.org/z/zn44rdW6P
- avar 4y agoThe 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".
- smcameron 4y agoEdit: Nevermind. I had an extra space so my experiment was flawed. So, I tried it with gcc and clang, and neither did anything strange. Also tried -std=c99 and -std=c89. All behaved the same. I thought maybe my distro had some default option configured to turn of trigraphs/digraphs, but --verbose didn't seem to show anything suspicious. $ cat garbage.c # include<stdio.h> int main() { puts("What?? (really)"); return 0; } $ clang -std=c11 -o garbage garbage.c $ ./garbage What?? (really) $ clang -std=c99 -o garbage garbage.c $ ./garbage What?? (really) $ man gcc $ clang -std=c89 -o garbage garbage.c $ ./garbage What?? (really) $ gcc -std=c11 -o garbage garbage.c $ ./garbage What?? (really) $ gcc -std=c99 -o garbage garbage.c $ ./garbage What?? (really) $ gcc -std=c89 -o garbage garbage.c $ ./garbage What?? (really) $
- GrumpySloth 4y agoYou need to get rid of the space between “??” and “(“.
- merlincorey 4y agoI've only seen them recently in things like the IOCCC. [0] https://www.ioccc.org https://www.ioccc.org