c - 如何在c中显示输入的字母?

标签 c for-loop

我是c 编程 的新手。我已经为输入的字母创建了一个程序并最终显示了输入的字母..但它总是只显示最后的字母..请帮助..我知道它的简单问题但是我是初学者所以请帮助大家..

 #include<stdio.h>      
    int main()
    {
        char z;
        int a;
            printf("enter the no.");
            scanf("%d",&a);
            printf("the entered no. is:%d\n",a);
            int i;
            for(i=0;i<a;i++)
            {
                printf("enter the letters:");
                scanf("%s",&z);
            }
            printf("the entered letters are:");
            for(i=0;i<a;i++)
            {
                printf("%s\n",&z);
            }
            return 0;

    }

最佳答案

问题:

  1. 您应该使用 %c(对于字符)而不是 %s(对于字符串)。

  2. 使用字符数组存储多个字符。阅读数组 here .

  3. 在第二个 for 循环中从 printf() 中删除 &

试试这个:

int main()
{
    char z[10];  //can hold 10 characters like z[0],z[1],z[2],..
    int a;
    printf("enter the no.");
    scanf("%d",&a);
    printf("the entered no. is:%d\n",a);
    int i;
    for(i=0;i<a;i++)
    {
        printf("enter the letters:");
        scanf("%c",&z[i]);
    }
    printf("the entered letters are:");
    for(i=0;i<a;i++)
    {
        printf("%c\n",z[i]);
    }
    return 0;
}

关于c - 如何在c中显示输入的字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22370713/

相关文章:

使用 argp 库完全隐藏选项

python - 打印以逗号分隔的项目列表,但在最后一项之前使用单词 "and"

ios - 我能否以编程方式在 `for` 循环中设置条件?

r - 如何在 R 中的列表列表上应用函数?

ios - fatal error : Index out of range for in loops swift 3

c - 如何针对内存使用优化 GCC 编译?

c - 在 XPATH (libxml2) 中省略前缀会导致段错误

c - 来自 GCC 内联汇编的系统调用

C getline 函数不按指定读取行

windows - 对文件夹中的所有文件执行命令