简体   繁体   English

如何在C中将scanf()用于多维char数组

[英]How to use scanf() for multidimensional char array in C

I'm trying this code for entering values into multidimensional char array in C. 我正在尝试使用此代码在C中将值输入多维char数组。

The code: 编码:

char s[2][2];
    char TRUE = '1';

for (i =0; i < 2; i++)
{
    for (j = 0; j < 2; j++)
    {

        scanf("%c\n", &s[i][j]);
        printf("%c\n", (char)s[i][j]);
        printf("###\n");
        if (s[i][j] == TRUE)
            printf("Char are equal\n");
        else
            printf("Not\n");
    }
}

The problem is that for that first scanf(), i'm getting Null value. 问题是对于第一个scanf(),我得到的是Null值。 This code works good only from the second scanf(). 此代码仅在第二个scanf()中有效。

The code purpose is to enter '1' and '0' values and then compare if the input is equal to TRUE ('1'). 代码的目的是输入“ 1”和“ 0”值,然后比较输入是否等于TRUE(“ 1”)。

There are many problems with scanf() by default. 默认情况下, scanf()有很多问题。 You should call it: 您应该称呼它为:

scanf("%c", &s[i][j]);
fflush(stdin);

or if fflush() isn't available on your operating system that would be help: 或者如果您的操作系统上没有fflush()会有所帮助:

do scanf("%c", &s[i][j]); while (getchar() != '\n');

Hope that helped :) 希望有帮助:)

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

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