gpt4 book ai didi

c - 如何重复 scanf() n 次?

转载 作者:太空狗 更新时间:2023-10-29 16:05:53 25 4
gpt4 key购买 nike

我要求用户输入两个整数,nx。之后,我需要向他们询问 a 变量 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com