tags:

views:

1196

answers:

3

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?

+7  A: 

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.

Greg Hewgill
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.
paxdiablo
I didn't really mean it was only the compiler. I edited to clarify.
Greg Hewgill
++forioccchumour!
Artelius
+2  A: 

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

ChrisW
+21  A: 

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

CTT