C 编程语言:strcmp(str1, str2) 的行为

发布于 2024-08-29 14:09:30 字数 246 浏览 8 评论 0原文

在 C 中,我有一个字符数组:

d[20] 

它被分配了值 'if' 和一个空终止字符:

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

strcmp(d,"if") 的值应该为 0 吗?为什么?

我希望 strcmp 返回值 0。当我运行它时,我得到的值是 -1

In C, I have a character array:

d[20] 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

一片旧的回忆 2024-09-05 14:09:30

如果您的意思是 d[0] = 'i'; d[1] = 'f'; d[2] = '\0';,那么是的,应该返回 0。d[2] = '/0' 将分配完全不同的东西,而你的字符串不会被 null 终止。至少不是你期望的那样 - strcmp 可能会进入杂草并开始吸泥。

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.

恬淡成诗 2024-09-05 14:09:30

@everyone:“/0”拼写错误是@Mark 在编辑原始问题时引入的。原件有正确的“\0”。你的大多数答案(以及关于OP的假设)都被误导了。

@mekasperasky 以下代码正确生成 0 的值。如果你能将它与你的个人代码进行比较并找到差异,那么你可能已经解决了你自己的问题。

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;
}

@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;
}
没有心的人 2024-09-05 14:09:30

d[2] 应该是 '\0',而不是 '/0'

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

时光倒影 2024-09-05 14:09:30

空值由: 表示,

d[2]='\0'

而不是您编写的 /0 。

The null value is indicated by:

d[2]='\0'

and not /0 as you wrote.

魂牵梦绕锁你心扉 2024-09-05 14:09:30

正如其他答案所提到的, '/0' 不是空终止符, '\0' 是。

您可能认为在字符文字中指定多个字符可能会产生错误;不幸的是(至少在这种情况下)C 允许“多字符”文字字符 - 但多字符文字的确切行为是实现定义的(6.4.4.4/2“字符常量”):

整型字符常量的类型为 int。包含映射到单字节执行字符的单个字符的整型字符常量的值是解释为整数的映射字符表示形式的数值。包含多个字符(例如“ab”)或包含不映射到单字节执行字符的字符或转义序列的整型字符常量的值是实现定义的。

因此,您的 '/0' “字符”最终会成为某个实现定义的 int 值,该值在存储在 d[2] 中时会被截断。您的编译器可能会针对“多字符”文字生成警告,但这可能还取决于您为编译器提供的确切选项。

例如,我从 GCC 收到以下警告(我碰巧设置了 -Wall):

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

In my test with MSVC and MinGW, the value of '/0' is 0x00002f30,因此 d[2] = '/0' 最终等同于 d[2] = '0'

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'.

有深☉意 2024-09-05 14:09:30

这适用于我拥有的每个 C 编译器。我没想到会有所不同。它也适用于键盘

这在你的 C 编译器上有效吗?

#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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文