我想在 C 中制作一个井字游戏,但我不知道如何让 puts() 打印出一个角色
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(我来自 python),我过去学习的其中一件事 python 是尝试在 python。出于这个原因,我想我不妨尝试用 c 来制作 tic tac toe。所以,我首先要用我的代码完成的是将井字游戏板上的每个位置存储为一个字符(即 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()
中的“s”代表“字符串”。您应该使用 putchar()
来输出单个字符。
您的示例代码存在一些问题(不包括它无法编译的事实):
C 字符串:以零结尾的字符数组
示例:
char[] mystring = "ABC";
将是一个包含 ['A', 'B', 'C', 0]
.
的 4 字符数组
一个C字符串字面量使用双引号("ABC"
); C 字符文字使用单引号 ('A'
)
printf 采用 STRING 参数 (double-quotes):
printf('%c', a1); // WRONG
printf("%c", a1); // CORRECT
puts() 打印字符串
putchar() 打印一个字符
我希望这能帮助你澄清一些事情:)
所以,我开始尝试学习 c(我来自 python),我过去学习的其中一件事 python 是尝试在 python。出于这个原因,我想我不妨尝试用 c 来制作 tic tac toe。所以,我首先要用我的代码完成的是将井字游戏板上的每个位置存储为一个字符(即 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()
中的“s”代表“字符串”。您应该使用 putchar()
来输出单个字符。
您的示例代码存在一些问题(不包括它无法编译的事实):
C 字符串:以零结尾的字符数组
示例:
的 4 字符数组char[] mystring = "ABC";
将是一个包含['A', 'B', 'C', 0]
.一个C字符串字面量使用双引号(
"ABC"
); C 字符文字使用单引号 ('A'
)printf 采用 STRING 参数 (double-quotes):
printf('%c', a1); // WRONG
printf("%c", a1); // CORRECT
puts() 打印字符串
putchar() 打印一个字符
我希望这能帮助你澄清一些事情:)