何时在C中使用纯字符类型 [英] When to use the plain char type in C

查看:92
本文介绍了何时在C中使用纯字符类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在普通C语言中,按照标准,有三种不同的字符类型:

In plain C, by the standard there are three distinct "character" types:


  • 普通 char 定义了谁的签名。

  • 签名的字符

  • 未签名的字符

  • plain char which one's signedness is implementation defined.
  • signed char.
  • unsigned char.

我们假设至少C99,其中 stdint.h 已经存在(因此您有 int8_t uint8_t 类型,作为具有显着宽度的有符号和无符号字符的推荐替代形式。)

Let's assume at least C99, where stdint.h is already present (so you have the int8_t and uint8_t types as recommendable alternatives with explicit width to signed and unsigned chars).

就我而言,现在似乎使用普通的 char 类型仅在需要接口标准库的功能(例如 printf )并且在所有其他情况下才真正有用(或必要)时,才避免使用。在实现上签名时使用 char 可能导致未定义的行为,并且出于任何原因,您都需要对此类数据进行任何算术运算。

For now for me it seems like using the plain char type is only really useful (or necessary) if you need to interface functions of the standard library such as printf, and in all other scenarios, rather to be avoided. Using char could lead to undefined behavior when it is signed on the implementation, and for any reason you need to do any arithmetic on such data.

例如,处理Unicode文本(或使用127以上的值表示字符的任何代码页)时,使用适当类型的问题可能最明显,否则可以将其作为纯C字符串处理。但是,相关的 string.h 函数都接受 char ,并且如果输入了此类数据 char ,这会在尝试解释它时产生问题,例如对于能够处理其编码的显示例程。

The problem of using an appropriate type is probably the most apparent when dealing for example with Unicode text (or any code page using values above 127 to represent characters), which otherwise could be handled as a plain C string. However the relevant string.h functions all accept char, and if such data is typed char, that imposes problems when trying to interpret it for example for a display routine capable to handle its encoding.

什么是最推荐的方法?这样的情况?除此之外,是否还有其他特殊原因,建议在 stdint.h 的适当固定值上使用 char

What is the most recommendable method in such a case? Are there any particular reasons beyond this where it could be recommendable to use char over stdint.h's appropriate fixed-width types?

推荐答案

char 类型用于字符和字符串。它是所有字符串处理函数期望并返回的类型。 (*)实际上,您永远不必对 char 进行算术运算,尤其是在签名会有所作为的那种运算法则上。

The char type is for characters and strings. It is the type expected and returned by all the string handling functions. (*) You really should never have to do arithmetic on char, especially not the kind where signed-ness would make a difference.

无符号字符是用于原始数据的类型。例如, memcpy() fread()解释其 void * 参数为无符号字符的数组。该标准保证,任何类型也可以表示为 unsigned char 的数组。任何其他转换都可能是信号通知,即触发异常。 (ISO / IEC 9899:2011,第6.2.6节类型的表示形式)。 (**)

unsigned char is the type to be used for raw data. For example memcpy() or fread() interpret their void * arguments as arrays of unsigned char. The standard guarantees that any type can be also represented as an array of unsigned char. Any other conversion might be "signalling", i.e. triggering exceptions. (ISO/IEC 9899:2011, section 6.2.6 "Representation of Types"). (**)

签名字符是当您需要 char 大小(用于算术)。

signed char is when you need a signed integer of char size (for arithmetics).

(*):<$ c中的字符处理功能$ c>< ctype.h> 对此有点奇怪,因为它们迎合了EOF(负数),因此将字符值强制到 unsigned char中范围(ISO / IEC 9899:2011,第7.4节字符处理)。但是由于可以保证 char 可以强制转换为 unsigned char 并返回,而不会丢失第6.2节中的信息.6...。。。

(*): The character handling functions in <ctype.h> are a bit oddball about this, as they cater for EOF (negative), and hence "force" the character values into the unsigned char range (ISO/IEC 9899:2011, section 7.4 Character handling). But since it is guaranteed that a char can be cast to unsigned char and back without loss of information as per section 6.2.6... you get the idea.

char 的签名会有所作为-比较功能如 strcmp()-标准要求对 char 进行解释格式为无符号字符(ISO / IEC 9899:2011,第7.24.4节比较功能)。

When signed-ness of char would make a difference -- the comparison functions like in strcmp() -- the standard dictates that char is interpreted as unsigned char (ISO/IEC 9899:2011, section 7.24.4 Comparison functions).

(**):实际上,很难看到将原始数据转换为 char 并返回表示如何完成相同操作未签名字符的消息将不会发出信号。但是 unsigned char 是标准的部分内容。 ;-)

(**): Practically, it is hard to see how a conversion of raw data to char and back could be signalling where the same done with unsigned char would not be signalling. But unsigned char is what the section of the standard says. ;-)

这篇关于何时在C中使用纯字符类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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