0
#include <stdio.h>

int main (void){
  char board[3][3];
  printf("Enter X or O>");
  for(int i=0; i<3; i++){
    for(int j=0; j<3; j++){
        scanf("%c", &board[i][j]);
    }
  }
  printf("Board: ");
  for(int i=0; i<3; i++){
    for(int j=0; j<3; j++){
        printf("%c ", board[i][j]);
    }
    printf("\n");
  }
}
/* X O X 
   O X O 
   X O O 
*/

I'm trying to make a 3x3 tic tac toe board using 2D arrays in C. This code works fine for an Int array but doesn't work as expected using char. How can I fix this?

hade34
  • 47
  • 3
  • Can you elaborate on "doesn't work as expected using char"? Are you getting an error? The wrong result? – Mureinik Apr 23 '21 at 09:59
  • 1
    "_doesn't work as expected_" is not a problem description. What happens? Why is that bad? What should happen instead? [Edit] to clarify your problem/question. – underscore_d Apr 23 '21 at 09:59
  • Use `scanf(" %c", &board[i][j]);` mind the space before `%c`. – anastaciu Apr 23 '21 at 10:00
  • It runs but prints the wrong result. It is only taking 5 inputs instead of 3x3=9 and printing in a weird way. – hade34 Apr 23 '21 at 10:20

0 Answers0