3

Possible Duplicate:
Why are there digraphs in C and C++?
What does the C ??!??! operator do?!

In C++ there are alternative tokens for [] and {}, among others.

E.g. the following code compiles:

%:include <stdio.h>
int main() <%
    printf("Hello World!");
}

I thought about it for a while but couldn't figure any applicability, is there any logical reason for this?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
roim
  • 4,780
  • 2
  • 27
  • 35

1 Answers1

7

They're a hangover from C, really. There were implementations of C in which not all characters were available (such as some variants of EBCDIC that have no square brackets).

The C99 rationale document, section 5.2.1.1 Trigraph sequences has this to say:

Trigraph sequences were introduced in C89 as alternate spellings of some characters to allow the implementation of C in character sets which do not provide a sufficient number of non-alphabetic graphics.

The characters in the ASCII repertoire used by C and absent from the ISO/IEC 646 invariant repertoire are #, [, ], {, }, \, |, ~, and ^

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 3
    It was a problem with the keyboards actually, not the character set. I distinctly remember older Apples not having { and } keys. – Mr Lister Oct 02 '12 at 04:24
  • @MrLister: Those were probably the same ones that lacked lower case (making writing C especially tricky). – Greg Hewgill Oct 02 '12 at 04:24
  • @GregHewgill True; I wasn't trying to imply that C ran on Apple ][s anyway... – Mr Lister Oct 02 '12 at 04:27
  • Actually, @MrLister, the C99 rationale states otherwise, it was specifically for the character set limitations of the ISO646 baseline. Apple's limitations may well have been severe - although no serious developer worked without the 80-column card, which gave you lowercase just fine. In any case, I remember using word processors on the II where inverse characters represented lowercase. That would be one ugly looking C program but it's doable. – paxdiablo Oct 02 '12 at 04:37
  • @paxdiablo Ooh, those things were expensive. It took me years before I finally had one, because I really had to save up to buy a second floppy drive first. – Mr Lister Oct 02 '12 at 04:43