2

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 default is C99, etc.

#include <stdio.h>
#include <string.h> 
int main (void)
{
  int len; 
  len = strlen ("??=");
  printf ("len=%d\n", len); 
  return 0;
}

Here is the result. In both cases submitted compiler option -w to suppress warnings:

$ gcc t.c -w
$ ./a.out
len=3

$ gcc t.c -ansi -w
$ ./a.out
len=1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Amazing User
  • 3,473
  • 10
  • 36
  • 75
  • 2
    suppressing warnings is never a good idea :) there is no smoke without fire. (still +1 for `int main (void)`) – Andreas Grapentin Feb 20 '14 at 20:40
  • 1
    I'm curious - did you just happen by chance to use `"??="` as a test string, were you running into weirdness in a larger program and narrowed it down to this, or did you already know that `"??="` was a trigraph but still weren't sure about the different results? – Michael Burr Feb 20 '14 at 21:15
  • 1
    @Michael Burr I deliberately used the trigraph. But I met them recently, so have not yet figured out the details – Amazing User Feb 20 '14 at 21:34

2 Answers2

2

When it is not in ansi mode gcc is not converting the trigrpah ??=(length 3) to #(length 1) which explains the length difference.

If you had not suppressed warnings gcc would warn that it is ignoring the trigraph in the non ansi mode(see it live):

warning: trigraph ??= ignored, use -trigraphs to enable [-Wtrigraphs]

You can read more about dialect options in the Options Controlling C Dialect section of the gcc docs. It says the following for -ansi:

[...] It also enables the undesirable and rarely used ISO trigraph feature. [...]

Trigraphs are covered in the draft C99 standard section 5.2.1.1 Trigraph sequences which says:

Before any other processing takes place, each occurrence of one of the following sequences of three characters (called trigraph sequences12)) is replaced with the corresponding single character.

and the following sequences include:

??=   #
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
2

"??=" is a trigraph. That's a very ancient idea to escape source code, if you can't enter in your terminal, what you want to enter.

"??=" is actual "#"

Jörg Beyer
  • 3,631
  • 21
  • 35