Finally! My joy knows no bounds.
Finally! My joy knows no bounds.
Much of the time, neither does a C compiler.
They seem pretty harmless, are they difficult to implement?
#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.
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");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".
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)
$