Code:
#include <stdio.h>
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: trigraph ??< converted to { [-Wtrigraphs]
??< puts("Hello Folks!"); ??>
^
source_file.c:8:30: warning: trigraph ??> converted to } [-Wtrigraphs]
But when I change the body of main
to:
<% puts("Hello Folks!"); %>
no warnings are thrown.
So, Why does the compiler warn me when using trigraphs, but not when using digraphs?
The warning is emitted only with --coverage enabled. By default, this warning is enabled and is treated as an error. -Wno-coverage-invalid-line-number can be used to disable the warning or -Wno-error=coverage-invalid-line-number can be used to disable the error.
gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.
By default, Fuchsia C/C++ code compiles with the flag -Wconversion , which prohibits implicit type conversions that may alter a value.
You can do this by specifying Werror=warning-name , which will cause that specific warning to generate an error. For example, a warning that I promote to an error is -Wreturn-type .
Because trigraphs have the undesirable effect of silently changing code. This means that the same source file is valid both with and without trigraph replacement, but leads to different code. This is especially problematic in string literals, like "<em>What??</em>"
.
Language design and language evolution should strive to avoid silent changes. Having the compiler warn about trigraphs is a good thing to have.
Contrast this with digraphs, which were new tokens that do not lead to silent changes.
This gcc document on pre-processing gives a pretty good rationale for a warning (emphasis mine):
Trigraphs are not popular and many compilers implement them incorrectly. Portable code should not rely on trigraphs being either converted or ignored. With -Wtrigraphs GCC will warn you when a trigraph may change the meaning of your program if it were converted.
and in this gcc document on Tokenization explains digraphs unlike trigraphs do not potential negative side effects (emphasis mine):
There are also six digraphs, which the C++ standard calls alternative tokens, which are merely alternate ways to spell other punctuators. This is a second attempt to work around missing punctuation in obsolete systems. It has no negative side effects, unlike trigraphs,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With