字符常量对于它的类型来说太长了 [英] Character constant too long for it's type

查看:115
本文介绍了字符常量对于它的类型来说太长了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试制作一个基于文本的游戏,但是对于item2-item6,它说,!多字符字符常量.字符常量对于其类型而言太长.从"int"到"char"的隐式转换将值从175174007更改为103.

I wanted to try to make a text based game, but for item2 - item6 it says, !multi-character character constant. character constant too long for its type. Implicit conversion from 'int' to 'char' changes value from 175174007 to 103.

#include <stdio.h>

int main()
{
int monster,lion;
char action,item1,item2,item3,item4,item5,item6;

action = 0;
monster = 5;
lion = 3;
item1 = 'lamp';
item2 = 'axe';
item3 = 'nothing';
item4 = 'nothing';
item5 = 'nothing';
item6 = 'nothing';

推荐答案

您正在将多字节字符常量分配给char类型,这是在您收到警告(具有实现定义的行为)的情况下.

You are assigning multi-byte character constants to char type, which is wht you get warnings (which has implementation-defined behaviour).

您可以使用指针数组来定义它们:

You can use an array of pointers to define them:

char *items[] = {"lamp","axe","nothing","nothing","nothing","nothing"};

并使用,例如:

printf("%s", items[1]); //would print "axe"

类似地,您将使用 strcmp()(而不是 == ).

Similarly for comparison, you would use strcmp() (not ==).

这篇关于字符常量对于它的类型来说太长了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆