C: How to compare two strings? -


this question has answer here:

edit: duplicate , i've flagged such. see [question] why "a" != "a" in c?

so i'm trying print out specific message depending on field within struct. field contains string "1".

whenever run printf("%s", record.fields[2]); output 1; i've no format warnings.

however, when check field against corresponding string (in case, "1"), fails check:

if (record.fields[2] == "1") {     printf("the field 1!"); } 

you need use strncmp compare strings:

if (strncmp(record.fields[2], "1", 1) == 0) ... 

you need compare zero, because strcmp returns 0 when 2 strings identical.

however, looks not comparing strings: rather, looking specific character inside string. in case, need use character constant instead of string literal (with single quotes):

if (record.fields[2] == '1') ... 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -