當提供了錯誤的參數個數的printf():如何在Visual Studio 2005中獲取printf警告?
printf("%s", "foo", "bar");
或者通過提供錯誤類型的參數:
printf("%d", "foo");
GCC能夠警告這些錯誤:
$ gcc -Wformat printf_too_many_arguments.c
printf_warnings.c: In function `main':
printf_warnings.c:5: warning: too many arguments for format
printf_warnings.c:5: warning: too many arguments for format
$ gcc -Wformat printf_argument_of_wrong_type.c
printf_warnings.c: In function `main':
printf_warnings.c:5: warning: format `%d' expects type `int', but argument 2 has type `char *'
printf_warnings.c:5: warning: format `%d' expects type `int', but argument 2 has type `char *'
如何使用Visual Studio 2005獲得這樣的警告?
- dave
gcc編譯器在其中有特殊的代碼,可以「瞭解」printf函數和格式字符串。據我所知,MS編譯器沒有這個。 – 2010-08-12 09:13:34