ANSI C Trigraph

このプログラムは普通にVisual C++コンパイルできたりする不思議。

??=/??/
*?=/??/
*/include<stdio.h>
int/*??*/main(int/**/T,char*e??(??))??<??/
printf("??/110ello,%s??/n",e??(T-T??));??>

■実行結果

C:\> cl a.c
Microsoft(R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

a.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:a.exe
a.obj

C:\> a.exe
Hello,a.exe

gccコンパイルしたい場合は -ansi オプションを使うか -trigraphs オプションをつける必要がある。

$ gcc -ansi a.c && ./a.out
Hello,./a.out
$ gcc -trigraphs a.c && ./a.out
Hello,./a.out

gccコンパイルオプションのデフォルトでは -trigraphs はオフになっている。

$ gcc a.c
a.c:1:1: 警告: trigraph ??= ignored, use -trigraphs to enable
a.c:1: error: 文法エラー が '?' トークンの前にあります
a.c:1:5: 警告: trigraph ??/ ignored, use -trigraphs to enable
a.c:2:5: 警告: trigraph ??/ ignored, use -trigraphs to enable
a.c:4:30: 警告: trigraph ??( ignored, use -trigraphs to enable
a.c:4:33: 警告: trigraph ??) ignored, use -trigraphs to enable
a.c:4:37: 警告: trigraph ??< ignored, use -trigraphs to enable
a.c:4:40: 警告: trigraph ??/ ignored, use -trigraphs to enable
a.c:5:9: 警告: trigraph ??/ ignored, use -trigraphs to enable
a.c:5:22: 警告: trigraph ??/ ignored, use -trigraphs to enable
a.c:5:29: 警告: trigraph ??( ignored, use -trigraphs to enable
a.c:5:35: 警告: trigraph ??) ignored, use -trigraphs to enable
a.c:5:40: 警告: trigraph ??> ignored, use -trigraphs to enable

■参考文献

  1. Digraphs and trigraphs - Wikipedia