例如:
console> please enter 3 digits: 1 2 3
我只知道如何使用scanf
接受1位数:
scanf("%d", &space);
答案 0 :(得分:8)
您可以使用scanf
读取多个数字int a, b, c;
scanf("%d %d %d", &a, &b, &c);
答案 1 :(得分:4)
你试过了吗?
scanf("%d %d %d", &num1, &num2, &num3);
如果你知道你只想要这三个,那将会很好。如果你想要一个变量号,你需要在循环中完成它,例如:
#include <stdio.h>
int main (void) {
int i, ch, rc, val, count;
// Loop until we get a valid number.
rc = 0;
while (rc != 1) {
printf ("Enter count: "); fflush (stdout);
rc = scanf(" %d", &count);
// Suck up all characters to line end if bad conversion.
if (rc != 1) while ((ch = getchar()) != '\n');
}
// Do once for each number.
for (i = 1; i <= count; i++) {
rc = 0;
while (rc != 1) {
printf ("\nEnter #% 2d: ", i); fflush (stdout);
rc = scanf(" %d", &val);
if (rc != 1) while ((ch = getchar()) != '\n');
}
printf ("You entered %d\n", val);
}
return 0;
}
运行它会给你:
Enter count: 5
Enter # 1: 10
You entered 10
Enter # 2: 20
You entered 20
Enter # 3: 30
You entered 30
Enter # 4: 40
You entered 40
Enter # 5: 99
You entered 99