什么STRCMP()正好在C回报? [英] What does strcmp() exactly returns in c?

查看:214
本文介绍了什么STRCMP()正好在C回报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C写了这个code:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&math.h中GT;
#包括LT&;&string.h中GT;诠释的main()
{
    字符字符串1 [20];
    字符字符串2 [20];
    的strcpy(字符串1Heloooo);
    的strcpy(字符串2Helloo);
    的printf(%d个,STRCMP(字符串1,字符串));
    返回(0);
}

我的问题是要安慰 ASCII 值之间的打印值1或差0 \\ 0 字符即111?在本网站,它是写,这应该给了放111,但是当我运行在我的笔记本电脑,它显示1。为什么呢?


解决方案

CP preference.com文档


  INT STRCMP(为const char * LHS,为const char *右);


  
  

返回值


  
  

      
  • 如果LHS负值出现在字典顺序RHS之前。


  •   
  • 零,如果LHS和RHS比较平等的。


  •   
  • 如果LHS正值出现在字典顺序RHS后。


  •   

正如你可以看到它只是说,负,零或正。你不能指望别的了。

您链接的网站是不是不正确。它会告诉你的返回值是< 0 == 0 > 0 ,它给出了一个例子,说明它的输出。它不会告诉输出的 111

I wrote this code in C:

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

int main()
{
    char string1[20];
    char string2[20];
    strcpy(string1, "Heloooo");
    strcpy(string2, "Helloo");
    printf("%d", strcmp(string1, string2));
    return(0);
}

My question is should console print value 1 or difference between ASCII values of o and \0 character i.e 111 ? On this website , it is written that this should give out put 111 but when I run it in my laptop, it shows 1 . Why ?

解决方案

From the cppreference.com documentation

int strcmp( const char *lhs, const char *rhs );

Return value

  • Negative value if lhs appears before rhs in lexicographical order.

  • Zero if lhs and rhs compare equal.

  • Positive value if lhs appears after rhs in lexicographical order.

As you can see it just says negative, zero or positive. You can't count on anything else.

The site you linked isn't incorrect. It tells you that the return value is < 0, == 0 or > 0 and it gives an example and shows it's output. It doesn't tell the output should be 111.

这篇关于什么STRCMP()正好在C回报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆