Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

No mention of trigraphs? They are one of my favourite obscure C language features that I've never used.

Excerpt from GCC man page:

  Trigraph:       ??(  ??)  ??<  ??>  ??=  ??/  ??'  ??!  ??-
  Replacement:      [    ]    {    }    #    \    ^    |    ~
Missing backslash on your keyboard? No problem, just type ??/ instead.


More than once have I written something like this

  if(condition)
    printf("WTF??! value: %d", value);
...only to have the compiler nag me about it. That's pretty much the only situation I've come across trigraphs :)


Related to trigraphs are the alternative logical operator keywords like `and` and `or`. I'm surprised people don't use them more often because they're nicer to read than && and ||. In C, you must #include <iso646.h> but I think they're standard keywords.

C++ code example on Godbolt: https://godbolt.org/z/ED6tXK

https://en.cppreference.com/w/cpp/language/operator_alternat...


Hmmm... I would not prefer using "and" and "or" because that syntactic sugar obscures whether the bitwise or logical operations are intended. You get really used to reading && and || as "and" and "or" in your head after the first two decades of C programming : )


I can safely say that I have never #included iso646.h in my life.


After long debates (since IBM needs them in EBCDIC machines, like z/Series) they were dropped from C++ (I believe C++17, but might already be C++14) so if you use them in a header this might cause incompatibilities.


IIRC these are a legacy of BCPL


The story I heard was that the trigraphs were added during standardization because ISO 646, the international version of ASCII, did not require the characters [ \ ] { | }.

Fun fact: ISO 646 is also the reason that IRC allows these characters in nicknames. IRC was created in Finland, and the Finnish national standard had placed the letters Ä Ö Å ä ö å at those code points.

Edit: That doesn't explain the trigraphs for # ^ ~. I'm guessing some EBCDIC variants lacked those. Or some other computer vendor on the committee still supported some other legacy character set.


I had to use them once on a truly ancient amber screen serial terminal that lacked { and } on the keyboard. That was back in the 90s and the terminal was completely obsolete at the time but I needed to write a tiny hack program to solve an immediate problem. I remember only knowing about them in the first place from an odd compiler warning I'd seen on a different program.


They weren't in K&R. I recall a story about a standardization meeting, on the way to ANSI C, where representatives from some European(?) country that didn't have some of the necessary punctuation on their country-specific keyboards, essentially snuck the trigraphs into the spec when the other representatives weren't looking.


Trigraphs are also necessary on IBM Z if you don't want to (or can't) switch from most standard EBCDIC code pages to the special C code page




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: