C中的可变参数scanf [英] variadic scanf in C
问题描述
我很好奇能否在C语言中实现scanf
的可变版本.我的意思是,如果输入为push (1 2 3)
,则scanf
可以将其解析为%s %d %d %d
就像scanf("%s (%d)", string, some_list)
.
I was curious if it's possible to implement a sort of variadic version of scanf
in C. What I mean is if the input is push (1 2 3)
, scanf
would be able to parse that into %s %d %d %d
with something like scanf("%s (%d)", string, some_list)
.
它将使用%d
的所有实例并将它们(按顺序)附加到列表中...
It would take all instances of %d
and append them (in order) to the list...
我在说话吗?
对于指定的输入,string == "push"
和some_list == [1, 2, 3]
.
推荐答案
不是 vscanf,vssscanf,vsfcanf 是您要找的东西吗?从C99开始可以使用它们.
Isn't vscanf, vssscanf, vsfcanf is what you are looking for? They are avaialble from C99 onwards.
Here is a decription and example of usage of vscanf function. Note that C being a compiled and a strongly typed language, you need to give format specifiers properly. Doing something like what higher languages dos is going to involve more work (but is possible because features are inturn written in C).
这篇关于C中的可变参数scanf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!