c++ 为什么strcmp返回int而不是char?

栏目: C++ · 发布时间: 7年前

内容简介:更一般地,int是优选的,用于传递和返回简单的数值,因为它被定义为这样的值的“自然”大小,并且在一些平台上比较小的类型更有效.代码日志版权声明:翻译自:http://stackoverflow.com/questions/30487800/why-strcmp-returns-int-but-not-char
据我所知,变量类型(如char,int等)之间的唯一区别是它们占用的内存量.我猜他们在调节他们所持有的变量代表什么方面没有任何作用.如果是这样,在 here

,我看到以下strcmp:

The strcmp function compares the string s1 against s2, returning a  value that has the same sign as the difference between the first  differing pair of characters (interpreted as unsigned char objects,  then promoted to int).

我想问为什么结果提升到int?由于字符被比较,它们的差异适用于所有情况下的字符.那么结果是不是在结果的最后简单地追加0的结果呢?那么为什么要这样做呢?

更一般地,int是优选的,用于传递和返回简单的数值,因为它被定义为这样的值的“自然”大小,并且在一些平台上比较小的类型更有效.

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/30487800/why-strcmp-returns-int-but-not-char


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learning PHP, MySQL, and JavaScript

Learning PHP, MySQL, and JavaScript

Robin Nixon / O'Reilly Media / 2009-7-21 / USD 39.99

Learn how to create responsive, data-driven websites with PHP, MySQL, and JavaScript - whether or not you know how to program. This simple, streamlined guide explains how the powerful combination of P......一起来看看 《Learning PHP, MySQL, and JavaScript》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具