1

I want to input 16 characters in an array one by one...

#include<stdio.h>
void main(){
 int i,j;
 char a[4][4];
printf("Enter Values in array : ");
for ( i=0 ; i<=3 ; i++ )
{
for ( j=0 ; j<=3 ; j++ )
{
    printf("a[%d][%d] : ",i,j);
    scanf("%c",&a[i][j]);
}}
for ( i=0 ; i<=3 ; i++ )
{
for ( j=0 ; j<=3 ; j++ )
{
    printf("a[%d][%d] : %c\n",i,j,a[i][j]);
}}}

and the output is

a[0][0] : q
a[0][1] : a[0][2] : w
a[0][3] : a[1][0] : e
a[1][1] : a[1][2] : r
a[1][3] : a[2][0] : t
a[2][1] : a[2][2] : y
a[2][3] : a[3][0] : u
a[3][1] : a[3][2] : i
a[3][3] :

why cant I input in a[0][1],a[0][3] and so on....why are they being skipped... and also please tell a better method to make this work...

Sabre.Tooth
  • 179
  • 1
  • 3
  • 9

3 Answers3

6

scanf() leaves the newline characters in the input buffer which is consumed by the subsequent calls.

Tell scanf() to skip the whitespaces.

scanf(" %c",&a[i][j]); // Notice the space in the format string

The space in the format specifier makes scanf() ignore any white space characters before reading a character (for %c)

P.P
  • 106,931
  • 18
  • 154
  • 210
2

The answer by KingsIndian explains the issue.

As a solution I would use getchar() or getwchar() (see man 3 getchar). By reading a character ad a time you can:

  1. check whether the character you read belongs to the type you are expecting (see ctype.h or wctype.h)

  2. discard the ones you don't want like blanks, CRs, LFs tabs and the likes.

EnzoR
  • 2,523
  • 2
  • 16
  • 19
2

this is the problem when taking character input in C Language. when we type a character and press ENTER key then the ASCII value of ENTER would become the value for next scanf.

you are required to flush the stdin buffer, and for that you should write fflush(stdin).

 #include<stdio.h>

    void main(){

    int i,j;
    char a[4][4];
    printf("Enter Values in array : ");

    for ( i=0 ; i<=3 ; i++ )
    {
    for ( j=0 ; j<=3 ; j++ )
    {
        printf("a[%d][%d] : ",i,j);
        scanf("%c",&a[i][j]);
        fflush(stdin);// will clear the input buffer stdin

    }
    }
    for ( i=0 ; i<=3 ; i++ )
    {
    for ( j=0 ; j<=3 ; j++ )
    {
        printf("a[%d][%d] : %c\n",i,j,a[i][j]);
    }
    }
    }
Mayank Tiwari
  • 2,770
  • 4
  • 25
  • 46
  • 2
    The C standard only defines the behaviour of fflush for output streams. fflush(stdin) invokes undefined behaviour. – Nigel Harper Apr 13 '13 at 10:25
  • Many times before i have read the concept of **fflush(stdin)** in **"Let Us C"** book, and when i used it in my code to solve similar problems it worked well.... – Mayank Tiwari Apr 13 '13 at 10:28
  • It's a common misconception even amongst those who think they know the language well enough to teach or write about. It does work in a lot of cases but it's definitely not guaranteed to work. See http://stackoverflow.com/questions/2979209/using-fflushstdin for the relevant extract from the standard. – Nigel Harper Apr 13 '13 at 11:12
  • Thanks a lot sir, my teacher taught me to use it while taking character input and i have also read the same concepts in a book that i have mentioned. you have cleared my concepts and now i will not teach this concept to my students in future..... Once Again Thanks A Lot Sir.... – Mayank Tiwari Apr 14 '13 at 13:22