%n
存储输入字符串中已经处理成相关参数的字符个数;在这种情况下read
会得到这个值。我稍微重写了您的代码,以转储代码执行时每个变量发生的情况:
#include <stdio.h>
int main(int argc, char **argv)
{
char *s = "12 34 567";
int read=-1, cur = 0, x = -1, call=1;
printf("Before first call, s='%s' cur=%d x=%d read=%d\n", s, cur, x, read);
while(sscanf(s+cur, "%d%n", &x, &read) == 1)
{
cur += read;
printf("After call %d, s='%s' cur=%d x=%d read=%d\n", call, s, cur, x, read);
call += 1;
}
}
产生以下
Before first call, s='12 34 567' cur=0 x=-1 read=-1
After call 1, s='12 34 567' cur=2 x=12 read=2
After call 2, s='12 34 567' cur=5 x=34 read=3
After call 3, s='12 34 567' cur=9 x=567 read=4
分享和享受。