如何在 Visual Studio 2005 中获取 printf 警告?
当向 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 *'
How to get such warnings with Visual Studio 2005?
——戴夫
When providing the wrong number of arguments to printf():
printf("%s", "foo", "bar");
or when by providing arguments of the wrong type:
printf("%d", "foo");
gcc is able to warn about these mistakes:
$ 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 *'
How to get such warnings with Visual Studio 2005?
-- dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在使用 Visual Studio 2005 时使用 cppcheck (http://cppcheck.sourceforge.net/) 来检测不匹配提供给 printf/wprintf 的参数数量与所需参数的数量之间。
不幸的是,它不检查类型匹配,但这是一个开始。
I use cppcheck (http://cppcheck.sourceforge.net/) when working with Visual Studio 2005 which detects mismatches between the number of parameters provided to printf/wprintf and the number of parameters required.
Unfortunately it doesn't check the types match, but it's a start.
不幸的是 MSVC/Visual Studio 不支持这一点。
另请参阅 MSVC 的 __attribute__((format(printf, 1, 2))) ?
Unfortunately MSVC/Visual Studio does not support this.
See also __attribute__((format(printf, 1, 2))) for MSVC?
您将需要额外的软件来执行此操作。看一下 PC-Lint (http://www.gimpel.com/)。它可以找到这些类型的错误(以及更多[潜在的]错误)。
You will need additional software to do this. Take a look at PC-Lint (http://www.gimpel.com/). It can find these kinds of errors (and much more [potential] errors as well).