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
5
votes
5 answers

Print ?? and !! in different sequence will show different output

I had found a strange output when I write the following lines in very simple way: Code: printf("LOL??!\n"); printf("LOL!!?\n"); Output: It happens even the code is compiled under both MBCS and UNICODE. The output varies on the sequence of "?"…
wengseng
  • 1,330
  • 1
  • 14
  • 27
4
votes
2 answers

Meaning of character literals containing trigraphs for non-representable characters

On a C compiler which uses ASCII as its character set, the value of the character literal '??<' would be equivalent to that of '{', i.e. 0x7B. What would be the value of that literal on a compiler whose character set doesn't have a {…
supercat
  • 77,689
  • 9
  • 166
  • 211
4
votes
2 answers

Trigraph characters

c99 standard 5.2.1.1 Trigraph sequences 2 EXAMPLE The following source line printf("Eh???/n"); becomes (after replacement of the trigraph sequence ??/) printf("Eh?\n"); It's saying that it will replace the trigraph sequence, but it's not . It's…
Omkant
  • 9,018
  • 8
  • 39
  • 59
2
votes
2 answers

how can i skip those warnings? C++

Code Added: bool CHARACTER::SpamAllowBuf(const char *Message) { if (!strcmp(Message, "(?˛´c)") || !strcmp(Message, "(μ·)") || !strcmp(Message, "(±a≫Y)") || !strcmp(Message, "(AA??)") || !strcmp(Message, "(≫c¶?)") || !strcmp(Message, "(?đłe)") ||…
2
votes
2 answers

Digraph and trigraph can't work together?

I'm learning digraph and trigraph, and here is the code which I cannot understand. (Yes, I admit that it's extremely ugly.) This code can compile: #define _(s) s%:%:s main(_(_)) <% __; %>t This code can compile, too: #define _(s)…
nalzok
  • 14,965
  • 21
  • 72
  • 139
2
votes
5 answers

Are trigraphs required to write a newline character in C99 using only ISO 646?

Assume that you're writing (portable) C99 code in the invariant set of ISO 646. This means that the \ (backslash, reverse solidus, however you name it) can't be written directly. For instance, one could opt to write a Hello World program as…
user824425
2
votes
2 answers

Why MSVC compiler converts "??-" sequence to "~" in string literals?

I have a hard coded string in my code (which should be used as a file mask), but compiler always changes the "??-" sequence to "~", for example: const wchar_t textW[] = L"test-??-??-??.txt"; The testW will be "test-~~??.txt" (without quotes). The…
Mar
  • 941
  • 1
  • 10
  • 18
2
votes
2 answers

Different results with ON/OFF modes of strict compliance with ANSI C

Why when the ON or OFF mode of strict compliance with ANSI C program produces different results? Compliance with strict about writing of the reasons that most modern industrial compilers default to some expansion of its own language, and some by…
Amazing User
  • 3,473
  • 10
  • 36
  • 75
1
vote
2 answers

Curious trigraph sequence thing about ansi C

What was is it the original reason to use trigraph sequence of some chars to become other chars in ansi C like: ??=define arraycheck(a, b) a??(b??) ??!??! b??(a??) becomes #define arraycheck(a, b) a[b] || b[a]
edgarmtze
  • 24,683
  • 80
  • 235
  • 386
1
vote
1 answer

Trigraphs not compiling with MS compiler?

I have a C++14 project withe the Microsoft compiler in Visual Studio 2019 and I'm trying to understand Digraphs and Trigraphs, so my code is a bit weird: #include "Trigraphs.h" void Trigraphs::assert_graphs() ??< // How does this ever compile…
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
1
vote
2 answers

Are digraphs transformed by a compiler and trigraphs transformed by a preprocessor?

I'm trying to understand both trigraphs and digraphs rather than use them. I've read that post and I understood that: Converting trigraphs to corresponding characters shall always be done by the preprocessor, before the actual compilation…
Cătălina Sîrbu
  • 1,253
  • 9
  • 30
1
vote
2 answers

Why are string literals parsed for trigraph sequences in Gnu gcc/g++?

Consider this innocuous C++ program: #include int main() { std::cout << "(Is this a trigraph??)" << std::endl; return 0; } When I compile it using g++ version 5.4.0, I get the following diagnostic: me@my-laptop:~/code/C++$ g++ -c…
1
vote
1 answer

How to make a directed tripartite network from two bipartite networks?

Excuse me because I feel like this doubt should be simpler, but I can't find a satisfactory answer. I have two quantitative bipartite networks (that show the ecological relationships among A-B, and B-C). My problem is that I do not know how join…
1
vote
2 answers

Simple string output not as expected (new line appearing)

I have code equivalent to the following to print out a short string: #include #include int main(int argc, const char* argv[]) { std::string s = "finished??/not finished??"; std::cout << s << std::endl; return 0; } But the…
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0
votes
2 answers

Implementing trigraphs in a C89 compiler

I am attempting to write a simplistic C89 --> x86_64 compiler, based on this C89 standard draft, in C89, for learning's sake. So far, I am implementing translation phase 1. My understanding is that this consists of Reading the code into a…
Qaziquza
  • 111
  • 3
  • 3