【发布时间】:2019-11-06 09:24:41
【问题描述】:
我是 C 编程语言的新手,对函数 putchar() 和 getchar() 不太熟悉。我尝试编写一个代码来读取一组输入的字符并将其存储在一个数组中。 这是我的代码:
#include<stdio.h>
#include<ctype.h>
#define MAX_SIZE 100
int main(){
int i;
char c[MAX_SIZE]={0};
printf("Enter message:");
for(i=0;getchar()!='\n';i++){
c[i] = getchar(); /*looks like some error here that the compiler didn't found out.....*/
}
for(i=0;c[i]!='\n';i++){
putchar(c[i]);
}
return 0;
}
程序运行成功,但运行不佳。输出的结果是混乱的,完全没有意义。 我想知道我的代码有什么问题,因为它看起来对我来说没有错误(编译器也认为它正确)。我希望我能得到一个解释,除了得到一个正确的写法。
【问题讨论】:
标签: c arrays for-loop character getchar