Why are trigraphs generating errors in modern C++ compilers?
Asked Answered
M

4

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?

Megara answered 29/11, 2012 at 5:35 Comment(14)
What version of GCC are you calling "modern"?Cyano
@Cyano 4.7 is pretty modern.Whall
What does the ?? Operator do?Counterpoise
AFAIK they're off by default pretty much everywhere, you may find that turning on strict standard C++ conformance under GCC also enables them.Quincentenary
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. #1235082Ossetia
@Pubby-By the modern compiler, I meant the Turbo C++ compiler that is mostly used by educational institutions.Megara
@Rekire - '??!' is the trigraph that is used to represent '|' during the ages where the systems had no support for the '|' character.Megara
@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!Megara
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.Ossetia
@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. :)Suckerfish
No educational institutions use it. Using Turbo C++ for instruction disqualifies you from being an educational institution (unless your department is History).Catheterize
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,Megara
@Leushenko even nowadays you can still see lots of questions about Turbo C and Turbo C++ on SO, most of them are from Indian studentsGorgias
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++.Megara
C
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.

Chappie answered 2/12, 2012 at 8:33 Comment(7)
Note that trigraphs have been deprecated in C++14 and will be removed from C++17.Ottar
@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?Chappie
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 said that even though there are still issues with EBCDIC, they'll work around it separately from the standard. You can find Removing trigraphs??! too. The writing is on the wall — but I may be presumptuous in suggesting that C++14 is the wall that contained the writing.Ottar
@dyp: Yes, but does it say they're deprecated (or obsolescent, or whatever term the C++ committee uses)?Chappie
@dyp: Thanks for the information; I've updated the answer yet again.Chappie
@JonathanLeffler: See the more recent comments.Chappie
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).Ottar
O
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.

Ottar answered 29/11, 2012 at 5:43 Comment(0)
P
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

Peccable answered 29/11, 2012 at 5:39 Comment(0)
D
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.

Devisable answered 29/11, 2012 at 5:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.