Problem with String compare(strcmp) in C
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_LOG开发者_运维问答IN_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.
You don't need to use '=' sign after #define. You can read more here.
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)
精彩评论