87

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?

4
  • 6
    Verify my translation? %: is #, and <% %> is {}, and <: :> is []. Is this correct? Commented Jan 11, 2009 at 7:02
  • 11
    The real answer: because IBM was loud and insisted on forcing it on everyone. Commented Dec 31, 2014 at 5:57
  • 2
    Voting to reopen. That question is more specific than this (only about and and or). This one is posed on a more useful form and has more upvotes. Edit: should be a duplicate of: stackoverflow.com/questions/1234582/… instead. Commented May 9, 2016 at 15:06
  • 3
    The real answer: So you can write obfuscated code :-) Commented Oct 29, 2016 at 4:38

5 Answers 5

81

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

4
  • 3
    Non-ASCII keyboards were not a problem. Sure it looked odd, but... main(int argc,char *argvÄÅ) ä printf("HelloÖn"); å Commented May 6, 2019 at 10:44
  • 1
    @robinr That it "looked odd" is exactly why the Scandinavians asked for digraphs and there was resistance on the C standards committee (X3J11, of which I was a member) to add them. Commented May 21, 2023 at 17:49
  • 1
    @Pryftan The question is about digraphs, not trigraphs. As for "if wikipedia says this", you should read the Wikipedia article to see what it says rather than speculating. (The Wikipedia article is not wrong but this answer is.) Commented May 21, 2023 at 17:51
  • 1
    As a Scandinavian I just changed to ASCII glyphs and wrote funny looking (to me) emails instead. Commented Sep 7, 2023 at 14:08
28

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.

5
  • 6
    Not 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. Commented Jan 11, 2009 at 6:27
  • I didn't really mean it was only the compiler. I edited to clarify. Commented Jan 11, 2009 at 6:51
  • 2
    No, 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). Commented Feb 28, 2014 at 8:18
  • 5
    The mention of IOCCC added significant value to this answer for me. Commented Jul 4, 2014 at 14:17
  • 1
    @Pryftan The question is about digraphs, not trigraphs. I was on X3J11 and was directly involved in the discussions. My statement is correct. Commented May 21, 2023 at 17:40
18

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

3
  • The DEC-10 and 20 generally used standard ASCII for all text files, including source code. The 6-bit character set was typically only used in metadata like file and directory names. Commented Dec 8, 2023 at 11:27
  • Your link is dead Commented Jul 26, 2024 at 18:17
  • The new link is archive.org/details/enf-ascii Commented Dec 22, 2024 at 13:27
17

I think it's because some of the keyboards on this planet might not have keys like '#' and '{'.

1

They were created as a simpler alternative to trigraphs according to the article on Wikipedia.

I.e., for 5 trigraphs ??(, ??), ??<, ??>, ??=, the replacing digraphs were supplied: <:, :>, <%, %>, %:. This happened in 1994.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.