问题在C字符数组的输入和输出 [英] Problem with character array input and output in C
本文介绍了问题在C字符数组的输入和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了下面code读取一个字符数组并打印。
I wrote the following code to read a character array and print it.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void read_array(char a[],int n);
void print_array(char a[],int n);
int main(void)
{
char a[100];
int n;
printf("\nEnter n:");
scanf("%d",&n);
printf("\nEnter the characters:");
read_array(a,n);
printf("\nThe array now is: ");
print_array(a,n);
getch();
return 0;
}
void read_array(char a[],int n)
{
int i;
for(i=0;i<n;i++)
scanf("%c",&a[i]);
}
void print_array(char a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("a[%d]=%c\n",i,a[i]);
}
输入:
Enter n:15
Enter the characters:xxxxx xxxxx
输出:
The array now is:
a[0]=
a[1]=x
a[2]=x
a[3]=x
a[4]=x
a[5]=x
a[6]=
a[7]=
a[8]=
a[10]=
a[11]=x
a[12]=x
a[13]=x
a[14]=x
凡在我输入 A [5]
到 A [9]
为空白字符。那么怎么来输出 A [0] =(空)
?
Where in my input a[5]
through a[9]
are blank characters. So how come in the output a[0]=(a blank)
?
推荐答案
您正在阅读中的第一个字符是你输入进入新行 15
。使用与fgets()
和的sscanf()
- 你会更快乐
The first character you're reading in is the newline you typed to enter the 15
. Use fgets()
and sscanf()
- you'll be much happier.
这篇关于问题在C字符数组的输入和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文