如何使用scanf读取由空格分隔的数字 [英] How to read numbers separated by space using scanf
问题描述
我想使用 scanf()
函数读取由空格分隔的数字(整数类型).我已阅读以下内容:
I want to read numbers(integer type) separated by spaces using scanf()
function.
I have read the following:
这对我帮助不大.
如何读取带有空格作为分隔符的数字.例如我有以下数字作为输入 2 5 7 4 3 8 18
现在我想将这些存储在不同的变量中.请帮忙.
How can I read numbers with space as delimiter. For e.g. I have following numbers as input 2 5 7 4 3 8 18
now I want to store these in different variables.
Please help.
推荐答案
我认为默认情况下 scanf 读取的值带有空格/输入.好吧,如果您要打印整数,则可以在 '%d' 之间提供空格.其他情况也一样.
I think by default values read by scanf with space/enter. Well you can provide space between '%d' if you are printing integers. Also same for other cases.
scanf("%d %d %d", &var1, &var2, &var3);
同样,如果您想读取逗号分隔值,请使用 :
Similarly if you want to read comma separated values use :
scanf("%d,%d,%d", &var1, &var2, &var3);
这篇关于如何使用scanf读取由空格分隔的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!