8

Screen Shot of the Program with trigraphs compiled using Turbo C++

Even in the GCC Compilers, the trigraphs are not getting compiled without explicitly specifying the trigraph attribute.

#include<stdio.h>

int main()
 {
 int a=4;
 if((a==4) ??! (a==5))
   printf("\nHello world!");
 return 0;
 }

This program saved as try.c gets compiled in GCC Compiler only when we specify gcc -Wall -trigraphs try.c and it still shows warnings. Can you enlist some compilers that will treat and handle trigraphs without any errors or warnings?

Praveen Vinny
  • 2,372
  • 6
  • 32
  • 40
  • 1
    What version of GCC are you calling "modern"? – Pubby Nov 29 '12 at 05:41
  • @Pubby 4.7 is pretty modern. – Etienne de Martel Nov 29 '12 at 05:42
  • What does the ?? Operator do? – rekire Nov 29 '12 at 05:45
  • AFAIK they're off by default pretty much everywhere, you may find that turning on strict standard C++ conformance under GCC also enables them. – Richard Viney Nov 29 '12 at 05:50
  • 3
    To add to that: they're off by default because they're only necessary on brain-damaged systems that lack critical characters like `<` and `{`. You, and your code, are unlikely to ever encounter such a system. http://stackoverflow.com/questions/1234582/purpose-of-trigraph-sequences-in-c –  Nov 29 '12 at 06:28
  • @Pubby-By the modern compiler, I meant the Turbo C++ compiler that is mostly used by educational institutions. – Praveen Vinny Nov 29 '12 at 07:41
  • @Rekire - '??!' is the trigraph that is used to represent '|' during the ages where the systems had no support for the '|' character. – Praveen Vinny Nov 29 '12 at 07:45
  • @duskwuff - If you won't take it wrong, let me tell you that I don't wanna ignore and forget the constructs which had dominated the coding constructs, until a few decades ago! – Praveen Vinny Nov 29 '12 at 07:49
  • 5
    Trigraphs never "dominated". They were only ever needed in a few rare cases. Also, Turbo C++ is an ancient piece of junk, not a "modern compiler". (Sorry.) GCC 4 and Clang are modern compilers. –  Nov 29 '12 at 16:32
  • 8
    @PraveenVinny Turbo C++ is close to *two decades* old. *Good* educational institutions don't use it. It does not support namespaces, exceptions, and many other things that C++ is expected to have, and don't even get me started with C++11. :) – Masked Man Feb 13 '13 at 16:25
  • *No* educational institutions use it. Using Turbo C++ for instruction disqualifies you from being an educational institution (unless your department is History). – Alex Celeste Jun 13 '17 at 10:42
  • Still many schools in India use Turbo C++. But I had stopped using it some five years ago. Please see the date when this question was asked, – Praveen Vinny Jun 13 '17 at 11:49
  • @Leushenko even nowadays you can still see lots of questions about Turbo C and Turbo C++ on SO, most of them are from Indian students – phuclv Oct 06 '17 at 05:43
  • 1
    Yes. You are right. Its very unfortunate that many of the Indian schools and colleges has still got Turbo C++ in the labs where students practice C++. – Praveen Vinny Oct 07 '17 at 14:06

4 Answers4

12

Trigraphs were introduced by the 1989 ANSI C standard, and are retained in all later C standards (so far; the upcoming C23 standard will drop them). They also appear in the first ISO C++ standard, published in 1998, and in all later C++ standards up to and including C++14. (Trigraphs were removed in C++17. Thanks to Jonathan Leffler and dyp for tracking down the details.)

Quoting a draft of the C++17 standard:

Effect on original feature: Valid C ++ 2014 code that uses trigraphs may not be valid or may have different semantics in this International Standard. Implementations may choose to translate trigraphs as specified in C ++ 2014 if they appear outside of a raw string literal, as part of the implementation-defined mapping from physical source file characters to the basic source character set.

They are not an optional feature in either language (prior to C++17); all conforming compilers must support them and interpret them as specified by the respective language standard.

For example, if this program:

#include <stdio.h>
int main(void) {
    if ('|' == '??!') {
        puts("ok");
    }
    else {
        puts("oops");
    }
    return 0;
}

prints oops, then your compiler is non-conforming.

But many, perhaps most, C compilers are not fully conforming by default. As long as a compiler can be made to conform to the standard in some way, that's good enough as far as the standard is concerned. (gcc requires -pedantic and -std=... to do this.)

But even if a compiler is fully conforming, there's nothing in the standard that forbids a compiler from warning about anything it likes. A conforming C compiler must diagnose any violation of a syntax rule or constraint, but it can issue as many additional warnings as it likes -- and it needn't distinguish between required diagnostics and other warnings.

Trigraphs are very rarely used. The vast majority of development systems support directly all the characters for which trigraphs substitute: #, [, \, ], ^, {, |, }, ~.

In fact, it's likely that trigraphs are used accidentally more often than they're used correctly:

fprintf(stderr, "What just happened here??!\n");

Warning about trigraphs that might alter the meaning of a program (relative to the meaning it would have if the language didn't have trigraphs) is both permitted by the ISO standard and IMHO perfectly reasonable. Most compilers probably have options to turn off such warnings.

Conversely, for a C++17 compiler that doesn't implement trigraphs, it would be reasonable to warn about sequences that would have been treated as trigraphs in C++14 or earlier, and/or to provide an option to support trigraphs. Again, an option to disable such warnings would be a good thing.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 2
    Note that trigraphs have been deprecated in C++14 and will be removed from C++17. – Jonathan Leffler May 11 '15 at 12:55
  • @JonathanLeffler: Thanks, updated. I see in the n4296 C++17 draft that trigraphs are removed, but haven't found a publicly available C++14 draft that says they're deprecated. Do you have a reference? – Keith Thompson May 11 '15 at 15:22
  • 1
    No; I don't have a C++14 draft that says they're deprecated. I understood that they were, but haven't direct proof. I did a Google search on 'c++14 trigraphs' and found the paper where [IBM](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4210.pdf) said that even though there are still issues with EBCDIC, they'll work around it separately from the standard. You can find [Removing trigraphs??!](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4086.html) too. The writing is on the wall — but I may be presumptuous in suggesting that C++14 is the wall that contained the writing. – Jonathan Leffler May 11 '15 at 15:37
  • @dyp: Yes, but does it say they're deprecated (or obsolescent, or whatever term the C++ committee uses)? – Keith Thompson May 11 '15 at 17:42
  • @dyp: Thanks for the information; I've updated the answer yet again. – Keith Thompson May 11 '15 at 17:46
  • @JonathanLeffler: See the more recent comments. – Keith Thompson May 11 '15 at 17:47
  • Thanks. My understanding is that trigraphs are in C++14 (which is what your answer now says). I don't know whether the 'future directions' section (if it exists; I can't see one in C++11, and I don't have an official copy of C++14) says anything about trigraphs. The C11 standard has two 'future directions' sections, one for the language (6.11) and one for the library (7.31). – Jonathan Leffler May 11 '15 at 17:53
5

GCC is allergic to trigraphs. You have to explicitly enable them:

gcc -trigraphs ...

The GCC 4.7.1 manual says:

-trigraphs

Support ISO C trigraphs. The -ansi option (and -std options for strict ISO C conformance) implies -trigraphs.

It also says:

-Wtrigraphs

Warn if any trigraphs are encountered that might change the meaning of the program (trigraphs within comments are not warned about). This warning is enabled by -Wall.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

They might be turned off by default.

"Some compilers support an option to turn recognition of trigraphs off, or disable trigraphs by default and require an option to turn them on"

GCC might be one of the latter. Although it should by default ignore with warning, but in this case the ignoring might be causing the compile error

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
1

Trigraphs are converted at a very early stage of compilation, and can even be replaced in string literals. This makes errors that arise from trigraph translations very hard to detect (worst off if you consider debugging using a log and you find the output in your source).

The warning you see will help you quickly spot a possible culprit to track the source of the bug. Basically it's warning you that something might not be as you think it is.

Neowizard
  • 2,981
  • 1
  • 21
  • 39