【问题标题】: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我试图在 C 中制作井字游戏,但我不知道如何让 puts() 打印出一个字符
【发布时间】:2020-12-03 17:13:53
【问题描述】:

所以,我开始尝试学习c(我来自python),而我曾经学习python的一件事就是尝试在python中制作井字游戏。出于这个原因,我想我不妨尝试在 c 中制作井字游戏。所以,我想用我的代码首先完成的是将井字棋板上的每个位置存储为一个字符(将是 0 或 X),然后确保我找到了一种方法将它们打印到终端。您可以在我到目前为止制作的代码中看到这一点。 a1、a2、a3为前3,b1、b2、b3为中3,c1、c2、c3为后3:

a1 a2 a3
b1 b2 b3
c1 c2 c3

现在,我想确保我知道如何将一行井字游戏打印到命令行,您可以在底部看到puts(a1); puts(a2); puts(a3);。但是,当我运行这个程序时,我在终端中得到以下输出作为错误:

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 *);
                                         ^~~~

我也尝试过使用 printf 函数,就像 printf('%c', a1); 一样,但这似乎也不起作用。这是我当前的代码,任何帮助将不胜感激:

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

【问题讨论】:

  • puts 的签名是:int puts(const char *str)。你传给它一个字符串,而不是char。更改为以下内容(例如):char *a1 = "0";.
  • 尝试putchar输出一个字符。
  • 如果要打印单个字符,请使用putchar
  • 我已经学习了基础知识,使用这本书toves.org/books/cpy。另外,你能解释一下我如何详细打印一个 char 变量吗?我不太明白你想说什么。
  • 我建议您查看此类事物的数组,而不是每个单元格的命名变量。然后,您可以通过算法检查行、列和对角线。

标签: c char puts


【解决方案1】:

您的示例代码存在一些问题(不包括它无法编译的事实):

  • C 字符串:一个以零结尾的字符数组

    示例:

    char[] mystring = "ABC"; 将是一个包含 ['A', 'B', 'C', 0] 的 4 字符数组。

  • C 字符串文字使用双引号 ("ABC"); C 字符文字使用单引号 ('A')

  • printf 采用 STRING 参数(双引号):

    • printf('%c', a1); // WRONG
    • printf("%c", a1); // CORRECT
  • puts() 打印一个字符串

  • putchar() 打印一个字符

我希望这有助于你澄清一些事情:)

【讨论】:

  • 谢谢,现在让我试试,我知道我需要一个 main() 函数,别担心 :D。
  • 对不起,我是 C 新手,没有意识到你真的需要半个 main 函数
【解决方案2】:

puts() 中的“s”代表“字符串”。您应该改用putchar() 来输出单个字符。

【讨论】:

    最近更新 更多