I'm a beginner in C and am currently experiencing some troubles invovling chars. I'm having an issue trying to enter values in a char matrix and then print it.
Here's my code:
#include <stdio.h>
#define N 3
int main( )
{
char arr[N][N]={{0}};
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
scanf("%c",&arr[i][j]);
}
}
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf("%c",arr[i][j]);
}
}
return 0;
}
There are two chars that are missing at the end of the output.
I don't know what I'm doing wrong and I would like to understand my mistake:
-Is it some kind of problem involving the scanf fonction? I've heard about the buffer before, is that related? Is the problem coming from the moment I press enter?
-Am I initializing my matrix in a wrong way?
-Is it better to use getchar() in this situation? If so, how can I manage to enter exactly N*N values and not more?
Thanks a lot. Jordan.