如何使用c中的scanf语句读取空间分隔的整数? [英] How to read integre numbers which are separeted by space using scanf statement in c?

查看:82
本文介绍了如何使用c中的scanf语句读取空间分隔的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数量范围各不相同

ex;

首次输入用户输入1 2 3 4 5 6 7



第二次输入1 2 3 4 5 6 7 8 9 63 44 32 12等





问题在于我想在c中的单个scanf语句中读取所有整数请帮助

the range of number is varies
ex;
at first input user enter 1 2 3 4 5 6 7

at second input 1 2 3 4 5 6 7 8 9 63 44 32 12 etc


the problem is that i want to read all integer in a single scanf statement in c please help

推荐答案

您无法在单个scanf语句中读取可变数量的输入值 - 如果你不知道用户输入的数字的数量,那么你需要:

1)读取整行,使用fgets

2)将其转换为单独的输入使用strtok

3)使用sscanf处理每个输入



虽然你可以说:

You can't read a variable number of input values in a single scanf statement - if you don't know the number of numbers the user will enter, then you need to:
1) Read the whole line, using fgets
2) Convert it to separate inputs using strtok
3) Process each input using sscanf

While you can say:
scanf("%d %d %d", &a, &b, &c);

没有可变数量输入的机制。

There is no mechanism for variable numbers of inputs.


这篇关于如何使用c中的scanf语句读取空间分隔的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆