Why are there digraphs in C and C++?
Solution 1
Digraphs were created for programmers that didn't have a keyboard which supported the ISO 646 character set.
http://en.wikipedia.org/wiki/C_trigraph
Solution 2
I believe that their existence can be traced back to the possibility that somewhere, somebody is using a compiler with an operating system whose character set is so archaic that it doesn't necessarily have all the characters that C or C++ need to express the whole language.
Also, it makes for good entries in the IOCCC.
Solution 3
I think it's because some of the keyboards on this planet might not have keys like '#' and '{'.
Solution 4
The digraphs and trigraphs in C/C++ come from the days of six bit character sets used by the CDC6000 (60 bits), Univac 1108 (36 bits), DECsystem 10 and 20 systems (36 bits) each of which used a proprietary 64 character set not compatible with the ASA X3.4-1963 (Now know as ANSI X3.4-1963 "7-bit American National Standard Code for Information Interchange"). The latest revision is ANSI X3.4-1986.
Since these systems were incapable of representing all of the 96 graphical code points, many were omitted. In addition, X3.4 was coordinated with other National Standard Institutes (GBR, GER, ITA, etc) and there were code points in X3.4 which were designated as national replacement characters - the most obvious example is the # for the Britsh Pound symbol (obvious because the name of the # character is "pound sign" from it's conventional usage in US commerce - prior to the the evolution of Twitter) and the '{' '}' were also designated as national replacement characters.
Thus digraphs were introduced to provide a mechanism for those computer systems incapable of representing the characters, and also for data terminal equipment which assigned national replacement characters to the conflicting code points. Di/Tri-graphs have become a archaic artifact of computing history (a subject not taught in computer science these days).
An exhaustive paper on this subject can be found here: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.96.678&rep=rep1&type=pdf
Related videos on Youtube
Paschalis Siskos
Software is my livelihood, my hobby, my obsession. I breathe code, and want to instill in everybody I meet a little bit of how wonderful the art can be. I constantly strive to improve, to learn, and to create the cleanest code I know how. I am language agnostic, and believe every language has appropriate uses, though my favorite is C++.
Updated on April 06, 2020Comments
-
Paschalis Siskos about 4 years
I learned today that there are digraphs in C99 and C++. The following is a valid program:
%:include <stdio.h> %:ifndef BUFSIZE %:define BUFSIZE 512 %:endif void copy(char d<::>, const char s<::>, int len) <% while (len-- >= 0) <% d<:len:> = s<:len:>; %> %>
My question is: why do they exist?
-
Admin over 9 yearsThe real answer: because IBM was loud and insisted on forcing it on everyone.
-
Ciro Santilli OurBigBook.com about 8 yearsVoting to reopen. That question is more specific than this (only about
and
andor
). This one is posed on a more useful form and has more upvotes. Edit: should be a duplicate of: stackoverflow.com/questions/1234582/… instead. -
MD XF over 7 yearsThe real answer: So you can write obfuscated code
:-)
-
-
paxdiablo over 15 yearsNot necessarily the compiler, Greg. Some of the mainframe EBCDIC character sets don't have consistent characters for the square brackets, which rather stuffs up array processing. This is a limitation of the editor and/or terminal emulator more than the compiler itself.
-
Greg Hewgill over 15 yearsI didn't really mean it was only the compiler. I edited to clarify.
-
Jim Balter over 10 yearsNo, it has nothing to do with EBCDIC. These sequences were for the sake of Scandinavians who used some of the ASCII characters as language characters (so the symbols were different on the keycaps and in output).
-
Weston almost 10 yearsThe mention of IOCCC added significant value to this answer for me.
-
qqbenq almost 10 yearsI recently found this article, which states that digraphs in Pascal have a root not in keyboard limitations but punch-card character representation limits. And yes, Pascal is not C or C++, but I thought you may find it interesting.
-
robinr about 5 yearsNon-ASCII keyboards were not a problem. Sure it looked odd, but... main(int argc,char *argvÄÅ) ä printf("HelloÖn"); å