gpt4 book ai didi

c - 在这个简单的程序中没有得到想要的输出?

转载 作者:太空宇宙 更新时间:2023-11-04 07:02:18 25 4
gpt4 key购买 nike

#include<stdio.h>
int main()
{
int i;int n;
scanf("%d",&n);
char a[n];
for(i=0;i<n;i++)
scanf("%c",&a[i]);
for(i=0;i<n;i++)
printf("%c",a[i]);
return 0;
}

//字符串没有完全复制//scanf 将 enter 作为字符串,但为什么?

最佳答案

您的代码实际上按照应有的方式运行。输入 3abc 后,程序将打印出 abc。我猜你的目的是输入 3 然后按 enter 并输入 abc 并在再次按 enter 时 abc 应该被打印出来。为此,您必须调整第一个 scanf 调用。

#include<stdio.h>
int main()
{
int i;int n;
scanf("%d\n",&n); //add \n in order to read the "pressing enter"
char a[n];
for(i=0;i<n;i++)
scanf("%c",&a[i]);
for(i=0;i<n;i++)
printf("%c",a[i]);
return 0;
}

关于c - 在这个简单的程序中没有得到想要的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36529627/

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