Friday, April 30, 2010

Trigraphs

One of the surprising things to me during the reading of c++ standard were trigraphs.
A trigraph (Greek: tri- = three, -graph = write) is a combination of three symbols, most commonly letters used to represent a single speech sound.

Trigraph
Replacement
??=
 #
??/
\
??'
^
??(
[
??)
]
??!
|
??<
{
??>
}
??-
~

The trigraph sequences are used for compatibility of source code written in ISO-646 or EBCDIC charset.

Trigraphs have been proposed for removal in C++0x.
You can read about it in the paper N2910 that discusses trigraph deprecation.

Here is another interesting article - EBCDIC and the P-BIT (The Biggest Computer Goof Ever).

GNU C guide says: "You don't need this brain damage." The GCC compiler has trigraph support disabled by default.

??=include <stdio.h>
??=include <windows.h>

void main()
??<    
    unsigned char *a = new unsigned char??(2??);
    a??(0??) = 0x6; // 00000110 = 6
    a??(1??) = 0xc; // 00001100 = 12
    
    printf("%d or %d = %d\n", a[0], a[1], a[0] ??! a[1]); // 0110 | 1100 = 1110 = 14
    printf("%d xor %d = %d\n", a[0], a[1], a[0] ??' a[1]); // 0110 ^ 1100 = 1010 = 10
    printf("not 0x%08X = 0x%08X\n", 0x12345678, ??-0x12345678); // ~12345678 = EDCAB987

    printf("Trigraphs!!!\n"); 
    
    // comment ??/
    comment too!!!
??>

Support for trigraphs in Visual Studio 2010 is disabled by default. So use the /Zc:trigraphs compiler option to enable trigraphs support.

No comments:

Post a Comment