abnormal behavior of scanf [duplicate]

The problem is due to '\n' characters (on pressing Enter ) left behind by scanf.
One way to eat up these newline character is place a ' ' before %c in scanf;

 scanf(" %c",&map[i][j]);  
        ^
        |
      space

Another way is to use a loop to eat up all the \n by getchar()

  int ch;
  while((ch=getchar())!='\n' && ch != EOF );

Leave a Comment