Loose-Info.com
Last Update 2021/05/16
TOP - 各種テスト - gcc - 警告関連のオプション - フォーマット文字列関連のオプション - -Wno-format-extra-args

-Wno-format-extra-args
-Wformatが指定されている場合、printf、scanf形式の関数への過剰な引数について警告しない

テスト概要

-Wformatを指定した場合の、-Wno-format-extra-argsオプション有りと無しの出力を比較

実行環境

GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。

使用ファイル


sample.c
#include <stdio.h> int main(void) { int n1 = 1; int n2 = 2; int n3 = 3; /* 過剰な引数を指定したprintf()関数 */ printf("n1 = %d : n2 = %d\n", n1, n2, n3); /* フォーマット文字列と引数の不一致 */ printf("n1 = %f : n2 = %d\n", n1, n2); return 0; }

動作テスト

-Wformatを指定した場合の、-Wno-format-extra-argsオプション有りと無しの出力を比較

-Wno-format-extra-argsオプション無しで実行
$ gcc -Wformat sample.c sample.c: In function ‘main’: sample.c:10:9: warning: too many arguments for format [-Wformat-extra-args] printf("n1 = %d : n2 = %d\n", n1, n2, n3); 過剰な引数の指定による警告 ^~~~~~~~~~~~~~~~~~~~~ sample.c:13:16: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=] printf("n1 = %f : n2 = %d\n", n1, n2); フォーマット文字列と引数の不一致による警告 ~^ ~~ %d $

-Wno-format-extra-argsオプションを指定して実行
$ gcc -Wformat -Wno-format-extra-args sample.c sample.c: In function ‘main’: sample.c:13:16: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=] printf("n1 = %f : n2 = %d\n", n1, n2); ~^ ~~ %d $ 過剰な引数の指定による警告は出力されない