在C中,我可以使用scanf
读说3个整数用空格从标准输入这样的分离:如何扫描一行n个整数?
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
}
,如果我不知道有多少整数是前手排在哪?假定用户提供整数数量:
#include <stdio.h>
int main() {
int howManyIntegersToRead;
scanf("%d", &howManyIntegersToRead);
// Read in the integers with scanf(...);
}
我需要的malloc大小sizeof(int) * howManyIntegersToRead
字节数组。我如何实际将标准输入数据读入分配的内存?我无法构建一个带有howManyIntegersToRead
%ds的格式化字符串。那么,我可以,但是有一个更好的方法。
不占多行输入。 –
是的,'%d'跳过空格(包括换行符) –