CarcaPo1
CarcaPo1

Reputation: 21

Im trying to make a tic tac toe game in C, but I can't figure out how to make puts() print out a character

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);

Upvotes: 0

Views: 347

Answers (2)

paulsm4
paulsm4

Reputation: 121649

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 :)

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83527

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

Upvotes: 3

Related Questions