How to compare a char?

后端 未结 2 1247
悲&欢浪女
悲&欢浪女 2020-12-30 07:49

I am learning c. I have a question. Why doesn\'t my program work?

#include
#include
#include

char cmd;

void          


        
相关标签:
2条回答
  • 2020-12-30 08:33

    First of, in C single quotes are char literals, and double quotes are string literals. Thus, 'C' and "C" are not the same thing.

    To do string comparisons, use strcmp.

    const char* str = "abc";
    if (strcmp ("abc", str) == 0) {
       printf("strings match\n");
    }
    

    To do char comparisons, use the equality operator.

    char c = 'a';
    if ('a' == c) {
       printf("characters match\n");
    }
    
    0 讨论(0)
  • 2020-12-30 08:36

    cmd is a char type but "e" is a string not a char type,you should write like this if(cmd == 'e')

    0 讨论(0)
提交回复
热议问题