C 中的字符串比较(strcmp)问题
我是 C 新手。我想比较我使用 '#DEFINE' 和 char buf[256] 的字符串。 这是我的代码。
#define SRV_SHOWMENU "SRV_SHOWMENU"
#define SRV_LOGIN_TRUE = "SRV_LOGIN_SUC"
#define SRV_LOGIN_FAIL = "SRV_LOGIN_FAIL"
#define SRV_REGISTER_OK = "SRV_REGISTER_SUC"
#define SRV_REGISTER_FAIL = "SRV_REGISTER_FAIL"
char buf[256]; // buffer for client data
...
...
...
...
...
...
if(strcmp(buf,SRV_SHOWMENU) == 0 || strcmp(buf,SRV_REGISTER_FAIL) == 0)
{
}
我的 C 编译器告诉我系统错误“../src/server.c:417: error: Expected expression before '=' token”。但如果我更改为“if(strcmp(buf,SRV_SHOWMENU) == 0)”,则只需进行一次比较就可以了。
谢谢。
I'm newbie in C. I want to compare string that I use '#DEFINE' and char buf[256].
This is my code.
#define SRV_SHOWMENU "SRV_SHOWMENU"
#define SRV_LOGIN_TRUE = "SRV_LOGIN_SUC"
#define SRV_LOGIN_FAIL = "SRV_LOGIN_FAIL"
#define SRV_REGISTER_OK = "SRV_REGISTER_SUC"
#define SRV_REGISTER_FAIL = "SRV_REGISTER_FAIL"
char buf[256]; // buffer for client data
...
...
...
...
...
...
if(strcmp(buf,SRV_SHOWMENU) == 0 || strcmp(buf,SRV_REGISTER_FAIL) == 0)
{
}
My C compiler tell me the systax error that "../src/server.c:417: error: expected expression before ‘=’ token". But if I change to "if(strcmp(buf,SRV_SHOWMENU) == 0)" just one compare is ok.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
#define 后不需要使用“=”符号。您可以在此处阅读更多内容。
You don't need to use '=' sign after #define. You can read more here.
正如已经说过的,删除#defines中的
=
符号。有了
=
,预编译器将变成if(strcmp(buf,SRV_SHOWMENU) == 0 || strcmp(buf,SRV_REGISTER_FAIL) == 0)
到As already said, remove the
=
signs in the #definesWith the
=
in, the pre compiler will turnif(strcmp(buf,SRV_SHOWMENU) == 0 || strcmp(buf,SRV_REGISTER_FAIL) == 0)
into