开发者

C programming language: Behavior of strcmp(str1, str2)

In C, I have a character array:

d[20] 
开发者_如何学JAVA

Which is assigned the value 'if' with a null termination character:

d[0]='i'
d[1]='f'
d[2]='\0'

Should the value of strcmp(d,"if") be 0? Why?

I expect strcmp to return a value of 0. When I run it, I get the value of -1


If you mean d[0] = 'i'; d[1] = 'f'; d[2] = '\0';, then yes, that should return 0. d[2] = '/0' will assign something entirely different and your string won't be null terminated. At least not where you expect it to be - strcmp will probably head off into the weeds and start sucking mud.


@everyone: The '/0' typo was introduced by @Mark when he edited the original question. The original had the proper '\0'. Most of your answers (and assumptions about the OP) are misdirected.

@mekasperasky The following code correctly produces the value of 0. If you can compare it to your personal code and find the difference, you may have solved your own problem.

int main(int argc, char* argv[])
{
   char d[20] = {0};
   d[0] = 'i';
   d[1] = 'f';
   d[2] = '\0';

   int value = strcmp(d,"if");
   cout << value << endl;
   return 0;
}


d[2] should be '\0', not '/0'.


The null value is indicated by:

d[2]='\0'

and not /0 as you wrote.


As other answers have mentioned, '/0' is not a null termination character, '\0' is.

You might expect that specifying more than one character in a character literal might generate an error; unfortunately (at least in this case) C allows 'multi-character' literal characters - but the exact behavior of multi-character literals is implementation defined (6.4.4.4/2 "Character constants"):

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.

So your '/0' 'character' ends up being some implementation defined int value that gets truncated when stored in d[2]. Your compiler might generate a warning for 'multi-character' literals, but that would probably also depend on the exact options you give the compiler.

For example, I get the following warning from GCC (I happen to have -Wall set):

C:\temp\test.cpp:6:14: warning: multi-character character constant

In my tests with MSVC and MinGW, the value of '/0' is 0x00002f30, so d[2] = '/0' ends up being equivalent to d[2] = '0'.


This works on every C compiler I have. I did not expect differently. It also works on Codepad.

Does this work on your C compiler?

#include <stdio.h>
#include <string.h>

char d[20];

int main(void) {
   d[0]='i';
   d[1]='f';
   d[2]='\0';

   printf("Value of strcmp=%i\n\n",strcmp(d,"if"));  /* will print 0 */

   return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜