我要求用户输入两个整数, n x 。之后,我需要向他们询问 变量 n 次的值。我需要为每个值创建一个新的变量。我该怎么做?我真的一点儿都不知道。

另外,我需要在一行中完成,因此输入将是,例如 50 30 21 ,而不是

50
30
21

谢谢。
#include <stdio.h>

int main (void) {
  int a, n, x;
  int i = 0;

  scanf ("%d%d", &n, &x);

  scanf ("%d", &a); /* what should I do here? */
}

最佳答案

试试这个:

int arr[100]; // static allocation but you can also allocate the dynamically memory

printf("Enter the number for how many time repeat scanf()\n");
scanf("%d",&n);

for (int i = 0; i < n; i++)
{
    scanf("%d",&arr[i]);
}

关于c - 如何重复scanf()n次?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39887775/

10-13 08:18
查看更多