GCC は警告を発生させず、誤ったコードをコンパイルします。

GCC は警告を発生させず、誤ったコードをコンパイルします。
#include <stdio.h>

int main() {
  printf("Enter your name\n");
  char name[99];
  scanf("%d", name);
  printf("Hello %s\n", name);
}

この簡単なプログラムを実行して誤って%d。ただ出力ファイルを生成します。%sgcc

$ gcc greet.c
$ ls
greet.c a.out
$ 

そして、このコードをコンパイルするとclang警告が表示されます。パラメータが渡されていないかのように警告を表示する必要があることを確認してくださいgcc。私は最近UbuntuからDebianに切り替えましたが、これがいくつかの欠落している依存関係が原因であるかどうかはわかりません。clang

いくつかの追加情報

GCC version : gcc (Debian 8.3.0-6) 8.3.0
OS : Debian 10(Buster)

ベストアンサー1

GCCでは、フォーマット文字列のチェックは次のように制御されます。-Wformat、デフォルトでは有効になっていません。

-Wformat(または-Wall含む)を使用してコードを書くと、警告が発生します。

$ gcc -Wformat    630368.c   -o 630368
630368.c: In function ‘main’:
630368.c:6:16: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’ [-Wformat=]
        scanf("%d", name);
               ~^   ~~~~
               %hhd

(GCC 8を使用)または

$ gcc -Wformat    630368.c   -o 630368
630368.c: In function ‘main’:
630368.c:6:16: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’ [-Wformat=]
    6 |        scanf("%d", name);
      |               ~^   ~~~~
      |                |   |
      |                |   char *
      |                int *
      |               %hhd

(GCC 10を含む)。

-WformatUbuntuに付属のGCCでは、デフォルトでカスタム仕様が有効になっていますgcc -dumpspecs

*distro_defaults:
%{!fno-asynchronous-unwind-tables:-fasynchronous-unwind-tables} %{!fno-stack-protector:%{!fstack-protector-all:%{!ffreestanding:%{!nostdlib:%{!fstack-protector:-fstack-protector-strong}}}}} %{!Wformat:%{!Wformat=2:%{!Wformat=0:%{!Wall:-Wformat} %{!Wno-format-security:-Wformat-security}}}} %{!fno-stack-clash-protection:-fstack-clash-protection} %{!fcf-protection*:%{!fno-cf-protection:-fcf-protection}}

(特に%{!Wformat:%{!Wformat=2:%{!Wformat=0:%{!Wall:-Wformat} %{!Wno-format-security:-Wformat-security}}}})。

おすすめ記事