输入要使用重新present一个字节的ANSI(C89 / 90)C? [英] Type to use to represent a byte in ANSI (C89/90) C?

查看:124
本文介绍了输入要使用重新present一个字节的ANSI(C89 / 90)C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个标准的投诉的方法来重新present一个字节的ANSI(C89 / 90)C?我知道,很多时候,一个char恰好是一个字节,但我的理解是,这是不能保证的情况下。此外,还有stdint.h在C99标准,但C99前使用的是什么?

我很好奇这两个专8位,和一个字节(的sizeof(X)== 1)。


解决方案

字符总是字节,但它并不总是一个<强>字节的。字节是内存的最小可寻址单元(在大多数的定义),一个八位字节的内存8位单位。

这就是的sizeof(char)的总是 1 所有的实现,但 CHAR_BIT limits.h中定义了一个平台一个字节的大小,它并不总是8位。有16位和32位字节的平台,因此,字符将占用更多的位,但它仍然是一个字节。由于对字符要求的范围至少-127到127(或0到255),这将是的至少的8位在所有平台上。


  

ISO / IEC 9899:TC3


  
  

6.5.3.4 sizeof操作符


  
  

      
  1. ...

  2.   
  3. sizeof操作符产生的操作数(以字节为单位的 )的大小,这可能是一个前pression或类型的括号名称。 [...]

  4.   
  5. 当应用到具有类型的操作数字符 unsigned char型签署焦,(或合格网络版版本体),结果为 1 即可。 [...]

  6.   

重点煤矿。

Is there a standards-complaint method to represent a byte in ANSI (C89/90) C? I know that, most often, a char happens to be a byte, but my understanding is that this is not guaranteed to be the case. Also, there is stdint.h in the C99 standard, but what was used before C99?

I'm curious about both 8 bits specifically, and a "byte" (sizeof(x) == 1).

解决方案

char is always a byte , but it's not always an octet. A byte is the smallest addressable unit of memory (in most definitions), an octet is 8-bit unit of memory.

That is, sizeof(char) is always 1 for all implementations, but CHAR_BIT macro in limits.h defines the size of a byte for a platform and it is not always 8 bit. There are platforms with 16-bit and 32-bit bytes, hence char will take up more bits, but it is still a byte. Since required range for char is at least -127 to 127 (or 0 to 255), it will be at least 8 bit on all platforms.

ISO/IEC 9899:TC3

6.5.3.4 The sizeof operator

  1. ...
  2. The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. [...]
  3. When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. [...]

Emphasis mine.

这篇关于输入要使用重新present一个字节的ANSI(C89 / 90)C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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