This question already has answers here:
strcmp behaviour in 32-bit and 64-bit systems
(3 answers)
Closed 9 years ago.
So I have this (not the full code)
char list[1000][10];
strcpy(list[0],"ab");
printf("%d\n",strcmp(list[0],"ab"));
and the strcmp is returning 0. Can somebody explain why it's doing so?
Thanks in advance.
The strcmp method will return 0 if list[0] contains "ab" in this case.
It returns:
Returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.
strmp returns 0 when the strings match. Unless I am missing something, it's behaving as expected.
see: http://www.tutorialspoint.com/ansi_c/c_strcmp.htm
strcmp() does an ordinal comparison of the strings, not an equality test. The return value of 0 means the strings are equal!
If you want to test for equality use this pattern:
if (strcmp(s, "ab") == 0) {
// strings are equal
}
Because strcpy() is a function that copies the "right" string to the "left" string.
So, after strcpy(list[0],"ab");, the content of list[0] is "ab".
Then, they are equal strings, and strcmp returns 0, which means "equal".
Your String is matched that is why it is returning 0
do like this...
char list[1000][10];
strcpy(list[0],"ab");
if(strcmp(list[0],"ab")==0)
printf("Matched\n",);
As mentioned by Daniel, the returnd value is 0.
Taken from cplusplus.com
Returns an integral value indicating the relationship between the
strings: A zero value indicates that the characters compared in both
strings form the same string. A value greater than zero indicates
that the first character that does not match has a greater value in
str1 than in str2; And a value less than zero indicates the opposite.
You should also work with strncmp rather than with strcmp.
Related
My question is that how will strcmp() handle the following case:
strcmp("goodpassT", "goodpass");
I read that the comparison continues until a different character is found or null character (\0) is found in any of the strings. In the above case, when it encounters \0 for the second argument, will it just stop comparison, or will it still compare to the T character ? The return value is 1, but I'm not sure about the stopping condition.
The comparison is done using unsigned char. Thus the shorter string is smaller as its terminating 0 is smaller than other unsigned nonzero char in the longer string.
See http://port70.net/~nsz/c/c11/n1570.html#7.24.4p1
The answer for this function strcmp("goodpassT", "goodpass"); will be 1 only.The point upto which lengths of both the string are same will be compared on the basis of their ASCII value.
I have a struct here with something like:
char *sname;
........
players[i].sname
equalling "James".
I need to check for equality between values like so:
if (players[i].sname == 'Lee')
but am not having much luck. Is there a str* function I should be using or is there anyway to fix up my if statement?
The short answer: strcmp().
The long answer: So you've got this:
if(players[i].sname == 'Lee')
This is wrong in several respects. First, single-quotes mean "character literal" not "string literal" in C.
Secondly, and more importantly, "string1" == "string2" doesn't compare strings, it compares char *s, or pointers to characters. It will tell you if two strings are stored in the same memory location. That would mean they're equal, but a false result wouldn't mean they're inequal.
strcmp() will basically go through and compare each character in the strings, stopping at the first character that isn't equal, and returning the difference between the two characters (which is why you have to say strcmp() == 0 or !strcmp() for equality).
Note also the functions strncmp() and memcmp(), which are similar to strcmp() but are safer.
You should be using strcmp():
if (strcmp(players[i].sname, "Lee") == 0) { ...
Also note that strings in C are surrounded by double quotes: "". Single characters are surrounded by single quotes: ''. I'm not sure exactly what your compiler might be doing with 'Lee', but it's almost certainly not what you want.
You'd be looking for strcmp() from the header <string.h>.
Note that you need a string — 'Lee' is not a string but a multi-character constant, which is allowed but seldom useful, not least because the representation is defined by the compiler, not the C standard.
If you are looking to compare two strings — call the pointers to them first and second, then you write:
if (strcmp(first, second) == 0) // first equal to second
if (strcmp(first, second) <= 0) // first less than or equal to second
if (strcmp(first, second) < 0) // first less than second
if (strcmp(first, second) >= 0) // first greater than or equal to second
if (strcmp(first, second) > 0) // first greater than second
if (strcmp(first, second) != 0) // first unequal to second
This, in my view, makes it clear what the comparison is and so the notation should be used. Note that strcmp() may return any negative value to indicate 'less than' or any positive value to indicate 'greater than'.
You will find people who prefer:
if (strcmp(first, second)) // first unequal to second
if (!strcmp(first, second)) // first equal to second
IMO, they have the advantage of brevity but the disadvantage of being less clear than the explicit comparisons with zero. YMMV.
Be cautious about using strncmp() instead of strcmp(), which was suggested in one answer. If you have:
if (strncmp(first, "command", 7) == 0)
then if first contains "commander", the match will be valid. If that's not what you want but you want to use strncmp() anyway, you would write:
if (strncmp(first, "command", sizeof("command")) == 0)
This will correctly reject "commander".
strcmp works fine, provided one of the strings is null terminated. If both are max length and identical, the comparison will walk off the end, and most likely give a false negative result for the equality test. If one string is fixed inside of "" marks in the strcmp itself, that's not an issue, because we know it's null terminated.
If we are comparing two string variables, and we don't know if they are max length or not (maximum length ones are not null terminated), then we need to use strncmp, and use sizeof on one of them to get the third parameter. This solves the problem, because the sizeof is aware of the maximum length. Strcmp is 100% safe if one of the strings is a literal in double quotes.
I have a struct here with something like:
char *sname;
........
players[i].sname
equalling "James".
I need to check for equality between values like so:
if (players[i].sname == 'Lee')
but am not having much luck. Is there a str* function I should be using or is there anyway to fix up my if statement?
The short answer: strcmp().
The long answer: So you've got this:
if(players[i].sname == 'Lee')
This is wrong in several respects. First, single-quotes mean "character literal" not "string literal" in C.
Secondly, and more importantly, "string1" == "string2" doesn't compare strings, it compares char *s, or pointers to characters. It will tell you if two strings are stored in the same memory location. That would mean they're equal, but a false result wouldn't mean they're inequal.
strcmp() will basically go through and compare each character in the strings, stopping at the first character that isn't equal, and returning the difference between the two characters (which is why you have to say strcmp() == 0 or !strcmp() for equality).
Note also the functions strncmp() and memcmp(), which are similar to strcmp() but are safer.
You should be using strcmp():
if (strcmp(players[i].sname, "Lee") == 0) { ...
Also note that strings in C are surrounded by double quotes: "". Single characters are surrounded by single quotes: ''. I'm not sure exactly what your compiler might be doing with 'Lee', but it's almost certainly not what you want.
You'd be looking for strcmp() from the header <string.h>.
Note that you need a string — 'Lee' is not a string but a multi-character constant, which is allowed but seldom useful, not least because the representation is defined by the compiler, not the C standard.
If you are looking to compare two strings — call the pointers to them first and second, then you write:
if (strcmp(first, second) == 0) // first equal to second
if (strcmp(first, second) <= 0) // first less than or equal to second
if (strcmp(first, second) < 0) // first less than second
if (strcmp(first, second) >= 0) // first greater than or equal to second
if (strcmp(first, second) > 0) // first greater than second
if (strcmp(first, second) != 0) // first unequal to second
This, in my view, makes it clear what the comparison is and so the notation should be used. Note that strcmp() may return any negative value to indicate 'less than' or any positive value to indicate 'greater than'.
You will find people who prefer:
if (strcmp(first, second)) // first unequal to second
if (!strcmp(first, second)) // first equal to second
IMO, they have the advantage of brevity but the disadvantage of being less clear than the explicit comparisons with zero. YMMV.
Be cautious about using strncmp() instead of strcmp(), which was suggested in one answer. If you have:
if (strncmp(first, "command", 7) == 0)
then if first contains "commander", the match will be valid. If that's not what you want but you want to use strncmp() anyway, you would write:
if (strncmp(first, "command", sizeof("command")) == 0)
This will correctly reject "commander".
strcmp works fine, provided one of the strings is null terminated. If both are max length and identical, the comparison will walk off the end, and most likely give a false negative result for the equality test. If one string is fixed inside of "" marks in the strcmp itself, that's not an issue, because we know it's null terminated.
If we are comparing two string variables, and we don't know if they are max length or not (maximum length ones are not null terminated), then we need to use strncmp, and use sizeof on one of them to get the third parameter. This solves the problem, because the sizeof is aware of the maximum length. Strcmp is 100% safe if one of the strings is a literal in double quotes.
I have a Hexadecimal string hexa="ffffffff 0 0"; and an other string entered by the user from the console for example String="ffffffff 0 0";, i want to compare hexa to String in this case i want to have hexa equal String.
How can i do that,i'm working with C language i have searched around but i can't find an answer and i tried strcmp obviously did not work, I also tried sprintf(String2,"%s",hexa); then strcmp(String2,String) ;did not work either.
You most likely misinterpreted how strcmp works. It returns an integer which indicates the relationship between the strings. If you want to check whether they are equal, you should check if strcmp returned zero, like so:
if(strcmp(String,String2) == 0){
// Strings are equal
} else {
// Strings are not equal
}
I am new to C.
I have a code that reads first word from the line, Here is a piece of it:
scanf(Line, "%s", Word);
printf("%s\n", Word);
This code reads and prints the first word in the line. However, I need to compare the first word of the line to another word. Any suggestions?
strcmp(str1,str2)
Compares the C string str1 to the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
Returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2;
A value less than zero indicates the opposite.
Use strcmp(firststring, secondstring) it will return 0, 1, or -1.
If both strings are identical then the strcmp() function will return 0.
If the first character that does not match has a greater value in firststring, it will return 1.
Otherwise, it will return -1.
Take a look at strcmp
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1057537653&id=1043284385
You're looking to split words based on some character. In this case ' '. You should be finding the index of the space and then storing the two words into two new strings.