C 中的字符串比较(strcmp)问题

发布于 2024-09-16 17:05:59 字数 607 浏览 20 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魄砕の薆 2024-09-23 17:05:59

#define 后不需要使用“=”符号。您可以在此处阅读更多内容。

You don't need to use '=' sign after #define. You can read more here.

滥情稳全场 2024-09-23 17:05:59

正如已经说过的,删除#defines中的=符号。

#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) 
{ 

}

有了=,预编译器将变成if(strcmp(buf,SRV_SHOWMENU) == 0 || strcmp(buf,SRV_REGISTER_FAIL) == 0)

if(strcmp(buf,"SRV_SHOWMENU") == 0 || strcmp(buf,= "SRV_REGISTER_FAIL") == 0) 

As already said, remove the = signs in the #defines

#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) 
{ 

}

With the = in, the pre compiler will turn if(strcmp(buf,SRV_SHOWMENU) == 0 || strcmp(buf,SRV_REGISTER_FAIL) == 0) into

if(strcmp(buf,"SRV_SHOWMENU") == 0 || strcmp(buf,= "SRV_REGISTER_FAIL") == 0) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文