问题
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