c - 为什么它只存储两个字符而不是 4 个?

标签 c

<分区>

这是我的代码:

 #include<stdio.h>
    int main() 
    {

        char a[10][10];

        int i,n,m,j;

        n=2;
        m=2;


        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                scanf("%c",&a[i][j]);
            }

        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                printf("%c",a[i][j]);
            }
        }

    }

输出:

a b c d

一个b

进程返回 0 (0x0) 执行时间:3.965 秒 按任意键继续。

最佳答案

因为它们是存储,其中两个是空间。这就是您看不到它们的原因。

试试这个你就明白了

printf("[%c]",a[i][j]);

要解决这个问题你可以这样做

scanf(" %c",&a[i][j]);

这会消耗两个字符输入之间的空白字符。

来自标准:- 7.21.6.2

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier

还有这个

A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails.

关于c - 为什么它只存储两个字符而不是 4 个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47725884/

相关文章:

c - 查找主函数局部变量的地址

c - 如何使用c代码打开目录并将数据下载到其中

c - 如何查找命令行参数中字符的出现次数和位置

有条件的 SSE/AVX 根据比较添加或归零元素

c - 我正在尝试创建一个打开文件以在 C 中读取的函数

c++ - C - 使用 scanf() 时额外的换行输入

c - 隐藏结构上的单个项目

ios - iPhone 应用程序的 C 代码链接错误

c - 找到第一个可用位的最佳方法

c - 我如何在 C 中编写监视器代码?