为什么这两个程序在 VC++2008 中给出不同的输出?

发布于 2024-12-20 07:02:27 字数 1528 浏览 0 评论 0原文

为什么这两个程序在 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 技术交流群。

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

发布评论

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

评论(2

如此安好 2024-12-27 07:02:27

strcmpstrncmp 保证结果将包括:

零值表示两个字符串相等。
大于零的值表示第一个不匹配的字符在 str1 中的值大于在 str2 中的值;小于零的值表示相反。

返回的实际数字(1/-1 或 12/-13)是特定于实现的,并且可以是任何值。唯一重要的部分是两者都返回 0、小于零或大于零。在这方面,他们给出了相同的答案。

Both strcmp and strncmp provide the guarantee that the result will include:

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.

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.

音盲 2024-12-27 07:02:27

来自 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.

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