在scanf中的倒置参数() [英] Inverted arguments in scanf()
问题描述
我是(快速)写一些code和意外在倒参数scanf()的
:
我的char [] =ABC1 \\ t的;
scanf函数(我,%S);
与 GCC -Werror -Wall -Wextra
编译不抱怨这一位。显然,这code没有工作,但为什么不属于GCC告诉我,我倒的论点?不能把它检测到 I
不是一个格式字符串,或者第二个参数是不是一个商店能够类型?
修改结果
感谢您的见解是,看起来像我找到了答案,有一对 -Wformat
标志,使这个开捕(贴吧以下供参考)扭曲p>
哈!我找到了。打与 -Wformat = 2
标志GCC接住了。
发布的信息为他人参考:
下面的标志,我发现名单>
-Wformat
检查调用printf和scanf等,以确保提供参数的适当指定的格式字符串类型...
我曾以为 -Wall
不得不 -Wformat
在里面,这确实如此,但对真正重要的组成部分我刚才发现:
-Wformat包含在-Wall。用于在格式检测的一些方面,选择-Wformat-2000年,-Wno格式-额外指定参数时,-Wno格式零长度,多个控制-Wformat-非字面,-Wformat的安全性,和-Wformat = 2都可用,但不包括在-Wall
I was (quickly) writing some code and accidently inverted the arguments in scanf()
:
char i[] = "ABC1\t";
scanf(i, "%s");
Compiling with gcc -Werror -Wall -Wextra
doesn't complain about this one bit. Obviously, this code doesn't work, but why didn't gcc inform me that I inverted the arguments? Can't it detect that i
is not a format string, or that the second argument was not a store-able type?
EDIT
Thanks for the insight all, Looks like I found the answer, there was a twist on the -Wformat
flag that makes this "catchable" (posted it below for reference)
Ha! I found it. Hitting gcc with the -Wformat=2
flag caught it.
Posting the info for reference of others:
Here's the list of flags I found
-Wformat
Check calls to printf and scanf, etc., to make sure that the arguments supplied have types appropriate to the format string specified...
I had assumed -Wall
had -Wformat
in it, which it does, but the really important part about what I just found:
-Wformat is included in -Wall. For more control over some aspects of format checking, the options -Wformat-y2k, -Wno-format-extra-args, -Wno-format-zero-length, -Wformat-nonliteral, -Wformat-security, and -Wformat=2 are available, but are not included in -Wall.
这篇关于在scanf中的倒置参数()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!