为什么这两个程序在 VC++2008 中给出不同的输出?
为什么这两个程序在 VC++2008 中给出不同的输出?
毕竟,比较的是相同的字符串。
strcmp__usage.c
#include <stdio.h>
#include <string.h>
main()
{
char targetString[] = "klmnop";
printf ("Compare = %d\n", strcmp(targetString, "abcdef"));
printf ("Compare = %d\n", strcmp(targetString, "abcdefgh"));
printf ("Compare = %d\n", strcmp(targetString, "jlmnop"));
printf ("Compare = %d\n", strcmp(targetString, "klmnop"));
printf ("Compare = %d\n", strcmp(targetString, "klmnoq"));
printf ("Compare = %d\n", strcmp(targetString, "uvwxyz"));
printf ("Compare = %d\n", strcmp(targetString, "xyz"));
}
输出
Compare = 1
Compare = 1
Compare = 1
Compare = 0
Compare = -1
Compare = -1
Compare = -1
strncmp_usage.c
#include <stdio.h>
#include <string.h>
main()
{
char targetString[] = "klmnopqrstuvwxyz";
int n = 6;
printf ("Compare = %d\n", strncmp(targetString, "abcdef", n));
printf ("Compare = %d\n", strncmp(targetString, "abcdefgh", n));
printf ("Compare = %d\n", strncmp(targetString, "jlmnop", n));
printf ("Compare = %d\n", strncmp(targetString, "klmnop", n));
printf ("Compare = %d\n", strncmp(targetString, "klmnoq", n));
printf ("Compare = %d\n", strncmp(targetString, "uvwxyz", n));
printf ("Compare = %d\n", strncmp(targetString, "xyz", n));
}
输出
Compare = 10
Compare = 10
Compare = 1
Compare = 0
Compare = -1
Compare = -10
Compare = -13
Why do these two programs give different outputs in VC++2008?
After all, the same strings are compared.
strcmp__usage.c
#include <stdio.h>
#include <string.h>
main()
{
char targetString[] = "klmnop";
printf ("Compare = %d\n", strcmp(targetString, "abcdef"));
printf ("Compare = %d\n", strcmp(targetString, "abcdefgh"));
printf ("Compare = %d\n", strcmp(targetString, "jlmnop"));
printf ("Compare = %d\n", strcmp(targetString, "klmnop"));
printf ("Compare = %d\n", strcmp(targetString, "klmnoq"));
printf ("Compare = %d\n", strcmp(targetString, "uvwxyz"));
printf ("Compare = %d\n", strcmp(targetString, "xyz"));
}
Output
Compare = 1
Compare = 1
Compare = 1
Compare = 0
Compare = -1
Compare = -1
Compare = -1
strncmp_usage.c
#include <stdio.h>
#include <string.h>
main()
{
char targetString[] = "klmnopqrstuvwxyz";
int n = 6;
printf ("Compare = %d\n", strncmp(targetString, "abcdef", n));
printf ("Compare = %d\n", strncmp(targetString, "abcdefgh", n));
printf ("Compare = %d\n", strncmp(targetString, "jlmnop", n));
printf ("Compare = %d\n", strncmp(targetString, "klmnop", n));
printf ("Compare = %d\n", strncmp(targetString, "klmnoq", n));
printf ("Compare = %d\n", strncmp(targetString, "uvwxyz", n));
printf ("Compare = %d\n", strncmp(targetString, "xyz", n));
}
Output
Compare = 10
Compare = 10
Compare = 1
Compare = 0
Compare = -1
Compare = -10
Compare = -13
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
strcmp 和 strncmp 保证结果将包括:
返回的实际数字(1/-1 或 12/-13)是特定于实现的,并且可以是任何值。唯一重要的部分是两者都返回 0、小于零或大于零。在这方面,他们给出了相同的答案。
Both strcmp and strncmp provide the guarantee that the result will include:
The actual number returned (1/-1 or 12/-13) is implementation specific, and can be any value. The only portion that matters is that both return 0, less than zero, or greater than zero. In that respect, they provide the same answer.
来自 strncmp:
返回一个整数值,指示字符串之间的关系:
零值表示两个字符串中比较的字符全部相等。
大于零的值表示第一个不匹配的字符在 str1 中的值大于在 str2 中的值;小于零的值表示相反。
显然,对于不相等的字符,strcmp 始终返回 1 或 -1,而 strncmp 返回不相等字符之间的差。由于该行为未定义,因此不是问题。
From strncmp:
Returns an integral value indicating the relationship between the strings:
A zero value indicates that the characters compared in both strings are all 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.
Clearly strcmp always returns 1 or -1 for nonequal characters, while strncmp returns the difference between the nonequal characters. Since that behaviour is undefined it isn't a problem.