在c编程语言中,什么是占位符"%n"?以及下面的代码如何工作?
char s[150];
gets(s);
int read, cur = 0,x;
while(sscanf(s+cur, "%d%n", &x, &read) == 1)
{
cur+= read;
/// do sth with x
}
Run Code Online (Sandbox Code Playgroud)
- 此代码获取一行作为字符数组,然后扫描此字符数组中的数字,例如:如果
下次*s="12 34 567"
第一次
最后一次x = 12x = 34x = 567
从手册页
n Nothing is expected; instead, the number of characters consumed
thus far from the input is stored through the next pointer,
which must be a pointer to int. This is not a conversion,
although it can be suppressed with the * assignment-suppression
character. The C standard says: "Execution of a %n directive
does not increment the assignment count returned at the comple?
tion of execution" but the Corrigendum seems to contradict this.
Probably it is wise not to make any assumptions on the effect of
%n conversions on the return value.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
884 次 |
| 最近记录: |