c - 为什么我的程序在 C 语言中显示错误的输出

标签 c

我想要一个程序,其中用户将给出三个名称,它们将在结果中打印。

我声明了一个字符串“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/

相关文章:

c++ - 从午夜开始计算毫秒数

c - VS2015 gsl编译错误

c - 重新分配不工作

c - 简单循环缓冲区: Need to extend function to overwrite old data

c - 将 Malloc char 指针传递给函数

自定义工具链的CMake工具链文件(-c编译器选项不同含义)

c++ - 寻找幂的算法,即 n^p

c - 在 C 函数中使用枚举作为参数和返回类型

c - 如何从文件中读取最后一行?

c - 如何反转链表的顺序?