简体   繁体   English

用C语言扫描字符数组

[英]scan character array in C language

I want to scan a character array as input as shown below and I also want to get the number of rows from the user (it's a square matrix). 我想扫描一个字符数组作为输入,如下所示,并且我还想从用户那里获得行数(这是一个方矩阵)。

OOX OX
OXO 氧代
OOX OX

but when I scan it using the program below, it only scans two rows. 但是当我使用下面的程序扫描它时,它仅扫描两行。

#include <stdio.h>

int main()
{
    int size,i,j;
    printf("Enter size");
    scanf("%d",&size);

    int a[size][size];

    for(i=0;i<size;i++)
        for(j=0;j<size;j++)
            scanf("%c",&a[i][j]);

}

The output which I'm getting is: 我得到的输出是:

Enter size3 输入尺寸3
OOX OX
OXO 氧代

then it's not asking for the last row. 那么它并不需要最后一行。

Remember that the whitespace is not skipped here.. 请记住,此处不会跳过空格。

this would fix it, it would also consume the whitespace: 这将解决它,也将占用空白:

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

Sorry, I don't have enough reputation to comment. 抱歉,我没有足够的声誉来发表评论。 In addition to Yohannes' answer, consider what kind of input you are expecting. 除了Yohannes的答案之外,请考虑您期望什么样的输入。 You use an integer array to store values, but you tell scanf to expect characters with the "%c" . 您使用整数数组存储值,但是您告诉scanf使用"%c"字符。 Maybe use character array or take integers as input and get rid of type mismatch warning from the compiler. 也许使用字符数组或采用整数作为输入,并摆脱编译器的类型不匹配警告。

scanf("%d",&a[i][j]); to read integers 读取整数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM