Questions tagged [trigraphs]

Trigraphs in Standard C are three-character sequences starting with two question marks that are intended to help programmers using code sets without some of the characters used by C — {}[]#|^`\~ — which are not part of the invariant subset of ISO 646. Trigraphs were a part of C++ from C++98 through C++14, but were removed from C++17.

There are 9 trigraphs in Standard C:

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

Trigraphs are interpreted in translation phase 1, before the code is tokenized. This means that trigraphs can affect strings, character literals, and comments.

C++98 included trigraphs for compatibility with C90 and C99. Trigraphs were deprecated in C++14 and removed from C++7.

In 'Design and Evolution of C++', Stroustrup shows how Danish terminals might display a C++ program because Danish uses extra alphabetic characters in place of some ASCII characters:

int main(int argc, char* argvÆÅ)
æ
    if (argc < 1 øø *argvÆ1Å=='Ø0') return 0;
    printf("Hello, %søn",argvÆ1Å);
å

Encoded with trigraphs, that becomes:

int main(int argc, char* argv??(??))
??<
    if (argc < 1 ??!??! *argv??(1??)=='??/0') return 0;
    printf("Hello, %s??/n",argv??(1??));
??>

Written in full ASCII, the code is:

int main(int argc, char* argv[])
{
    if (argc < 1 || *argv[1]=='\0') return 0;
    printf("Hello, %s\n",argv[1]);
}

See also Wikipedia on Digraphs and Trigraphs.

33 questions
2526
votes
4 answers

What does the ??!??! operator do in C?

I saw a line of C that looked like this: !ErrorHasOccured() ??!??! HandleError(); It compiled correctly and seems to run ok. It seems like it's checking if an error has occurred, and if it has, it handles it. But I'm not really sure what it's…
Peter Olson
  • 139,199
  • 49
  • 202
  • 242
141
votes
9 answers

Purpose of Trigraph sequences in C++?

According to C++'03 Standard 2.3/1: Before any other processing takes place, each occurrence of one of the following sequences of three characters (“trigraph sequences”) is replaced by the single character indicated in Table 1.…
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
67
votes
3 answers

What is the meaning of `???-` in C++ code?

I saw the following code from some legacy codes: size_t a = 1 ???- 2 :0; What does the symbol ???- mean in C++? How should I understand it?
taocp
  • 23,276
  • 10
  • 49
  • 62
44
votes
8 answers

When were the 'and' and 'or' alternative tokens introduced in C++?

I've just read this nice piece from Reddit. They mention and and or being "Alternative Tokens" to && and || I was really unaware of these until now. Of course, everybody knows about the di-graphs and tri-graphs, but and and or? Since when? Is this a…
shoosh
  • 76,898
  • 55
  • 205
  • 325
42
votes
5 answers

Are digraphs and trigraphs in use today?

Given that there were once reasons to use digraphs and trigraphs in C and C++, does anyone put them in code being written today? Is there any substantial amount of legacy code still under maintenance that contains them? (Note: Here, "digraph" does…
rwallace
  • 31,405
  • 40
  • 123
  • 242
35
votes
1 answer

C++17 why not remove digraphs along with trigraphs?

C++17 removed trigraphs. IBM heavily opposed this (here and here) so there seem to be arguments for both sides of removal/non removal. But since the decision was made to remove trigraphs, why leave digraphs? I don't see any reasons for keeping…
bolov
  • 72,283
  • 15
  • 145
  • 224
24
votes
1 answer

Escape sequence for ? in c++

I was looking at the escape sequences for characters in strings in c++ and I noticed there is an escape sequence for a question mark. Can someone tell me why this is? It just seems a little odd and I can't figure out what ? does in a string. …
Anon
  • 5,103
  • 11
  • 45
  • 58
13
votes
2 answers

Is there a switch to disable trigraphs with clang?

I've got some (legacy) code that I'm building with clang for the first time. The code is something like: sprintf(buf, "%s <%s ????>", p1, p2); Clang gives the following warning (error with -Werror): test.c:6:33: error: trigraph converted to '}'…
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
10
votes
1 answer

Are trigraphs still valid C++?

We all know about the historical curiosity that is digraphs and trigraphs, but with all the changes made to C++ in recent years I'm curious: are they valid C++14? How about C++17?
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
9
votes
2 answers

suggest like google with postgresql trigrams and full text search

I want to do a text search like google suggestions. I'm using PostgreSQL because of the magical Postgis. I was thinking on using FTS, but I saw that it could not search partial words, so I found this question, and saw how trigrams works. The main…
jperelli
  • 6,988
  • 5
  • 50
  • 85
8
votes
1 answer

Why doesn't boost regex '.{2}' match '??'

I'm trying to match some chunks if interesting data within a data stream. There should be a leading < then four alphanumeric characters, two characters of checksum (or ?? if no shecksum was specified) and a trailing >. If the last two characters…
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
7
votes
4 answers

Why does GCC emit a warning when using trigraphs, but not when using digraphs?

Code: #include int main(void) { ??< puts("Hello Folks!"); ??> } The above program, when compiled with GCC 4.8.1 with -Wall and -std=c11, gives the following warning: source_file.c: In function ‘main’: source_file.c:8:5: warning:…
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
6
votes
1 answer

What is the meaning of these strange question marks?

I came across some weird-looking code. It doesn't even look like C, yet to my surprise it compiles and runs on my C compiler. Is this some non-standard extension to the C language and if so, what is the reason for it? ??=include int…
Lundin
  • 195,001
  • 40
  • 254
  • 396
6
votes
9 answers

Unknown meta-character in C/C++ string literal?

I created a new project with the following code segment: char* strange = "(Strange??)"; cout << strange << endl; resulting in the following output: (Strange] Thus translating '??)' -> ']' Debugging it shows that my char* string literal is…
Marius
  • 3,372
  • 1
  • 30
  • 36
5
votes
1 answer

Use of '??=', '??<' and '??>' in c

I was going through few interview questions and I came across example as below. I tried the example for simple input/output and also for some logic and it works without any problems. ??=include int main(void) ??< printf("Hello"); …
Jasmeet
  • 1,315
  • 11
  • 23
1
2 3