我想要一个程序,其中用户将给出三个名称,它们将在结果中打印。
我声明了一个字符串“name”,并将该“name”变量声明为数组(或者我想要)
但是当我运行程序时,我得到的输出很奇怪 它只接受一个名称,我认为这与数组声明有关,所以我更改了 'char name[3][20];'到“字符名称[20][3];”但什么都没有改变..
请帮助我..
程序:
#include<stdio.h>
#include<conio.h>
struct variable
{
char name[3][20];
}v;
void main()
{
int i=0,j=0;
clrscr();
printf("Enter Three names \n");
for(i=0;i<3;i++)
{
scanf("%c",&v.name[i]);
}
for(j=0;j<3;j++)
{
printf("%c\t",v.name[i]);
}
getch();
}
输出图像:-> here
最佳答案
%c 是 char 数据类型的格式字符串 要打印字符串,您需要使用 %s 格式字符串
# include <stdio.h>
int main(){
char name [3][20];
printf("Enter 3 names\n");
for(int i = 0; i < 3; i++){
scanf("%s", name[i]);
}
for(int i = 0; i < 3; i++){
printf("%s\n", name[i]);
}
return 0;
}
关于c - 为什么我的程序在 C 语言中显示错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46132650/