1

So, I've started to try to learn c (I came from python) and one of the things I used to learn python was to try to make tic tac toe in python. For this reason, I thought I might as well try to make tic tac toe in c. So, what i want to get done first with my code is storing each position on the tic tac toe board as a char (that will be either 0 or X), and then make sure that I have found a way to print those to the terminal. You can see this in the code I have made so far. a1, a2, and a3 are the top 3, b1, b2, and b3 are the middle 3, and c1, c2, and c3 are the bottom 3:

a1 a2 a3
b1 b2 b3
c1 c2 c3

Now, I wanted to make sure that I knew how to print a row of the tic tac toe board to the command line, which you can see at the bottom with puts(a1); puts(a2); puts(a3);. However, when I run this program, I get the following output in the terminal as an error:

ticktacktoe.c:17:1: warning: parameter names (without types) in function declaration
 char puts(a1); char puts(a2); char puts(a3);
 ^~~~
ticktacktoe.c:17:6: error: conflicting types for 'puts'    
 char puts(a1); char puts(a2); char puts(a3);
      ^~~~
In file included from ticktacktoe.c:1:0:
c:\mingw\include\stdio.h:677:41: note: previous declaration of 'puts' was here
 _CRTIMP __cdecl __MINGW_NOTHROW  int    puts (const char *);
                                         ^~~~
ticktacktoe.c:17:1: warning: parameter names (without types) in function declaration
 char puts(a1); char puts(a2); char puts(a3);
 ^~~~
ticktacktoe.c:17:21: error: conflicting types for 'puts'
 char puts(a1); char puts(a2); char puts(a3);
                     ^~~~
In file included from ticktacktoe.c:1:0:
c:\mingw\include\stdio.h:677:41: note: previous declaration of 'puts' was here
 _CRTIMP __cdecl __MINGW_NOTHROW  int    puts (const char *);
                                         ^~~~
ticktacktoe.c:17:1: warning: parameter names (without types) in function declaration
 char puts(a1); char puts(a2); char puts(a3);
 ^~~~
ticktacktoe.c:17:36: error: conflicting types for 'puts'
 char puts(a1); char puts(a2); char puts(a3);
                                    ^~~~
In file included from ticktacktoe.c:1:0:
c:\mingw\include\stdio.h:677:41: note: previous declaration of 'puts' was here
 _CRTIMP __cdecl __MINGW_NOTHROW  int    puts (const char *);
                                         ^~~~

I have tried to use the printf function as well, along the lines of printf('%c', a1); but that also seemed to not work. Here is my current code, and any help would be greatly appreciated:

#include<stdio.h>
#include<string.h>

//these are the variables for what is displayed on the tic tac toe board
char a1 = '0';
char a2 = '0';
char a3 = '0';

char b1 = '0';
char b2 = '0';
char b3 = '0';

char c1 = '0';
char c2 = '0';
char c3 = '0';

puts(a1); puts(a2); puts(a3);
CarcaPo1
  • 31
  • 4
  • 3
    The signature for `puts` is: `int puts(const char *str)`. You pass it a string, not a `char`. Change to the following (for example): `char *a1 = "0";`. – Fiddling Bits Aug 13 '20 at 19:44
  • 4
    Try `putchar` to output a char. – user3386109 Aug 13 '20 at 19:44
  • 3
    Use `putchar` if you want to print out a single character. – M. Nejat Aydin Aug 13 '20 at 19:44
  • I have learned the basics, using this book http://www.toves.org/books/cpy/. Also, can you explain how i could print a char variable in for detail? I don't exactly understand what your trying to say. – CarcaPo1 Aug 13 '20 at 19:46
  • Please check out the compiler warnings, and note that `puts()` always outputs a newline too. So it would only be useful for printing out an entire row of the game, if the row of cells is a string. – Weather Vane Aug 13 '20 at 19:47
  • so If I wanted to print a1, a2, and a3 on the same line, I would have to use printf, correct? how would I use print f to do this? – CarcaPo1 Aug 13 '20 at 19:49
  • 1
    I suggest you look at arrays for this kind of thing, instead of a named variable for each cell. You can then check rows, columns and diagonals algorithmically. – Weather Vane Aug 13 '20 at 19:50
  • The **first** problem is your "code" isn't **in a function**, so it is not even executable statements, just some broken declarations. The 2nd problem is your code does not match the **errors from the compiler**. The third gravest problem is not using an **array** for an array. The least of the problems is that you're not attempting to use the correct function. – Antti Haapala -- Слава Україні Aug 13 '20 at 19:54
  • You might want to start with a "hello world" program first. – Antti Haapala -- Слава Україні Aug 13 '20 at 19:54

2 Answers2

3

The "s" in puts() stands for "string". You should use putchar() instead to output a single character.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
3

There are a few problems with your example code (not counting the fact that it won't compile):

  • C string: a zero terminated array of characters

    EXAMPLE:

    char[] mystring = "ABC"; will be a 4-character array containing ['A', 'B', 'C', 0].

  • A C string literal uses double quotes ("ABC"); a C character literal uses single quotes ('A')

  • printf takes a STRING argument (double-quotes):

    • printf('%c', a1); // WRONG
    • printf("%c", a1); // CORRECT
  • puts() prints a string

  • putchar() prints a character

I hope that helps clarify a few things for you :)

paulsm4
  • 114,292
  • 17
  • 138
  • 190