- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
阅读 scanf
手册时我遇到了这一行:
An optional 'm' character. This is used with string conversions(
%s
,%c
,%[
),
有人可以用简单的例子来解释一下它的区别以及在某些情况下这种选项的需要吗?
最佳答案
C 标准在 scanf()
中没有定义这样的可选字符。格式。
GNU lib C 确实定义了一个可选的 a
这样指示(来自 scanf
的手册页):
An optional
a
character. This is used with string conversions, and relieves the caller of the need to allocate a corresponding buffer to hold the input: instead,scanf()
allocates a buffer of sufficient size, and assigns the address of this buffer to the corresponding pointer argument, which should be a pointer to achar *
variable (this variable does not need to be initialized before the call).The caller should subsequently
free
this buffer when it is no longer required. This is a GNU extension; C99 employs thea
character as a conversion specifier (and it can also be used as such in the GNU implementation).
手册页的注释部分显示:
The
a
modifier is not available if the program is compiled withgcc -std=c99
orgcc -D_ISOC99_SOURCE
(unless_GNU_SOURCE
is also specified), in which case thea
is interpreted as a specifier for floating-point numbers (see above).Since version 2.7, glibc also provides the
m
modifier for the same purpose as the a modifier. Them
modifier has the following advantages:
It may also be applied to
%c
conversion specifiers (e.g.,%3mc
).It avoids ambiguity with respect to the
%a
floating-point conversion specifier (and is unaffected bygcc -std=c99
etc.)It is specified in the upcoming revision of the POSIX.1 standard.
在线 Linux 手册页 http://linux.die.net/man/3/scanf仅将此选项记录为:
An optional 'm' character. This is used with string conversions (
%s
,%c
,%[
), and relieves the caller of the need to allocate a corresponding buffer to hold the input: instead,scanf()
allocates a buffer of sufficient size, and assigns the address of this buffer to the corresponding pointer argument, which should be a pointer to achar *
variable (this variable does not need to be initialized before the call). The caller should subsequentlyfree(3)
this buffer when it is no longer required.
Posix 标准在其 POSIX.1-2008 版本中记录了此扩展(请参阅 http://pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html ):
The
%c
,%s
, and%[
conversion specifiers shall accept an optional assignment-allocation characterm
, which shall cause a memory buffer to be allocated to hold the string converted including a terminating null character. In such a case, the argument corresponding to the conversion specifier should be a reference to a pointer variable that will receive a pointer to the allocated buffer. The system shall allocate a buffer as ifmalloc()
had been called. The application shall be responsible for freeing the memory after usage. If there is insufficient memory to allocate a buffer, the function shall seterrno
to [ENOMEM
] and a conversion error shall result. If the function returnsEOF
, any memory successfully allocated for parameters using assignment-allocation characterm
by this call shall be freed before the function returns.
使用此扩展,您可以编写:
char *p;
scanf("%ms", &p);
导致scanf
从标准输入解析单词并分配足够的内存来存储其字符以及终止 '\0'
。指向已分配数组的指针将存储到p
中。和scanf()
将返回1
,除非无法从 stdin
读取非空白字符.
其他系统完全有可能使用 m
对于类似的语义或完全不同的东西。非标准扩展是不可移植的,在标准方法麻烦、不切实际或完全不可能的情况下,应该非常小心地使用,并记录下来。
请注意,使用 scanf()
的标准版本确实不可能解析任意大小的单词。 :
您可以解析具有最大大小的单词,并且应指定在'\0'
之前存储的最大字符数。 :
char buffer[20];
scanf("%19s", buffer);
但这并不能告诉您在标准输入中还有多少个字符可供解析。无论如何,如果输入足够长,不传递最大字符数可能会引发未定义的行为,并且攻击者甚至可能使用特制的输入来危害您的程序:
char buffer[20];
scanf("%s", buffer); // potential undefined behavior,
// that could be exploited by an attacker.
关于c - %ms 和 %s scanf 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43237996/
我的运动需要一些帮助。 Question of exercise 这是我的代码: #include main() { int price,new_price; char code; printf(
这个问题在这里已经有了答案: scanf() leaves the newline character in the buffer (7 个答案) 关闭 5 年前。 我有这段代码(由于逻辑是家庭作业
我这里有一段代码,在输入带空格的字符串时会出现一些不需要的行为。例如 print illegal_argument。当输入包含多个单词时,我希望它只注册第一个单词。 目前发生的是: christian
我的测试输入是 [1,2,3] [4,5,6] [7,8,9] [9,9] 或者它可以是单个 2d 坐标之前的任意数量的 3d 坐标,我的代码是 #include #include #includ
这个问题在这里已经有了答案: What does space in scanf mean? [duplicate] (6 个答案) 关闭 7 年前。 像这样在scanf中加一个空格的目的是什么 sc
#include int main() { int n; scanf("%d", &n); printf(n); return 0; } 这是我的代码,我很愚蠢,可能
程序要求用户输入 2 名学生的姓名、ID 等,将数据写入文件并从文件中读回数据。 #include #include #include #include #include int main(
这个问题在这里已经有了答案: abnormal behavior of scanf [duplicate] (3 个答案) 关闭 8 年前。 今天我遇到了一个问题,当我使用两次接受字符作为输入的 s
我正在用 c 编写一个程序来读取文件的内容。代码如下: #include void main() { char line[90]; while(scanf("%79[^\
main() { int d,a; printf("Enter the digit :"); scanf("%d",&d); printf("Enter another
为什么为 scanf 输入添加多个句点会跳过下一个 scanf 函数。示例输入:b. b. int main (void) { char b[7] = " "; printf("Thi
我想我已经尝试过任何方法(刷新 stdin、scanf 以使用换行符等),但没有任何效果如我所希望的那样。由于某种原因,第三个 scanf 在以下代码中修改了第二个 scanf 中的变量: #incl
这个问题在这里已经有了答案: Scanf skips every other while loop in C (10 个答案) 关闭 6 年前。 我想做一个计算器,只是一个带有循环和基本操作的简单计
如果没有初始化 ("mod=0") ,这段代码进入死循环。我不明白为什么这段代码会循环,即使我使用了 getchar();删除缓冲区。当我先输入“1”,然后再输入“a”时,就会出现无限循环。谁能帮助我
当我为 scanf() 输入一个值时 它只是跳过紧随其后的第二个、第三个和任何其他 scanf()。 这是我的代码: #define _CRT_SECURE_NO_WARNINGS #include
#include #include void sstring(); int main() { char ch1[10],ch2; printf("Ent
这是一个计算房间内人员年龄的简单程序。我正处于初始阶段,现在我看到我不知道哪些变量(我的意思是我在 scanf 之前声明的变量,然后是 scanf 中的占位符)用于 scanf;如何选择和应用正确的变
这个问题在这里已经有了答案: scanf() leaves the newline character in the buffer (7 个答案) 关闭 5 年前。 我知道 scanf() 的用法,
我已经阅读了扫描集的行为。通过研究和测试,我遇到了一个问题。 难道 scanf("%[^\n]") 的行为与 scanf("%s") 一样吗? scanf("%s") 正在消耗字符,直到在 stdin
我遇到了一个问题,当我使用 scanf 将字符串存储到 char 指针中时,我有 3 个输入 - 姓名、姓氏和年龄,姓氏的最后一个 char 值被替换为年龄输出以更好地解释。 Q-riosity v0
我是一名优秀的程序员,十分优秀!