如何在 Visual Studio 2005 中获取 printf 警告?

发布于 2024-09-13 15:55:34 字数 754 浏览 3 评论 0原文

当向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平生欢 2024-09-20 15:55:34

我在使用 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.

大姐,你呐 2024-09-20 15:55:34

不幸的是 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?

风轻花落早 2024-09-20 15:55:34

您将需要额外的软件来执行此操作。看一下 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文