Character constant too long for it's type

萝らか妹 提交于 2019-12-20 04:22:40

问题


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

回答1:


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"};

and use, for example:

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

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



来源:https://stackoverflow.com/questions/32264839/character-constant-too-long-for-its-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!