4

code

int main()
{
     int n,m,i,j;char a[10][10];
     printf("enter n and m values\n");     
     scanf("%d%d",&n,&m);

     printf("enter array values");    
     for(i=0;i<n;i++)
        for(j=0;j<m;j++)
          scanf("%c",&a[i][j]);

     printf("the array is \n");
     for(i=0;i<n;i++)
        for(j=0;j<m;j++)
          printf("%d %d %c\t",i,j,a[i][j]);
}

Input

 Enter n and m values  
 4 5
 Enter characters 
 11111000001111100000

Output

0 0 

0 1 1   0 2 1   0 3 1   0 4 1   1 0 1   1 1 0   1 2 0   1 3 0   1 4 0   2 0 0    
2 1 1   2 2 1   2 3 1   2 4 1   3 0 1   3 1 0   3 2 0   3 3 0   3 4 0   

Error

If I give the value of n as 4 and m as 5 ,scanf does it job.

But while printing when the value of i is 0 and j is 0 it does not print anything.

Meanwhile a[0][1] prints the first input and a[0][2] prints second input and consecutively , so last input 0 is missing while printing.

Please explain why a[0][0] is avoided.

6
  • It's likely that there is a return character left in the buffer (because you scanf() only 2 integers) and it's being assigned to [0][0]. Try using fflush(stdout) before reading any characters with scanf. Commented Jul 1, 2014 at 14:33
  • Second result on google: gsamaras.wordpress.com/code/… Commented Jul 1, 2014 at 14:39
  • 3
    @sherrellbc I would not expect fflush(stdout) to have an effect on stdin. Commented Jul 1, 2014 at 14:44
  • 3
    @sherrellbc; No. It should not. Commented Jul 1, 2014 at 14:49
  • 1
    Hint: always check return value of any scanf function. If it's less than what you expect, and you don't handle it, it's not going to end well. Commented Jul 1, 2014 at 15:18

1 Answer 1

5

Previous scanf calls leave behind \n character in the input buffer which goes along with input on pressing Enter or Return key. scanf("%c",&a[i][j]); reads that \n on first iteration.

You need to flush your input buffer. Either place a space before %c in scanf

scanf(" %c", &a[i][j]);   
       ^A space before `%c` can skip any number of leading white-spaces

or you can use

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

NOTE: Will fflush(stdin) work in this case?

fflush is defined only for output streams. Since its definition of "flush" is to complete the writing of buffered characters (not to discard them), discarding unread input would not be an analogous meaning for fflush on input streams.

Suggested reading: c-faq 12.18.

Sign up to request clarification or add additional context in comments.

4 Comments

I looked at the documentation for scanf and it ignores any whitespace until the format (being a char in this case) is read in. What exactly does the space do?
@sherrellbc "ignores any whitespace until the format" is not correct. The precise function of scanf() is lengthy. Note: "%c", "%n", "%[]" do not skip leading white-space.
@chux, ah, my mistake. I read it assuming it would skip white space, but you must first include the whitespace character (as haacks does above) to trigger this feature.
Should be noted (not that you made this suggestion) that fflush should only be used on output streams (e.g. not stdin).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.