gpt4 book ai didi

c - sizeof 是否返回 C 中某种类型的字节数或八位字节数?

转载 作者:太空狗 更新时间:2023-10-29 16:30:36 25 4
gpt4 key购买 nike

简单地放入 C 和变体(不像 wuss java 及其虚拟机),不同目标上的原始类型的大小可能会有很大差异,除非您使用 stdint 中定义的固定宽度类型,否则真的无法保证.h,即便如此,您的实现也必须支持它们。

无论如何假设(因为在大多数现代机器上一个字节是一个八位字节,出于网络目的我假设(ASCII))sizeof 是以字节还是以八位字节为单位返回数据类型的大小?

最佳答案

答案:sizeof字节返回类型的大小。


示例:sizeof(char) 100% 保证为 1,但这并不意味着它是一个八位字节(8 位)。


由标准证明:

6.5.3.4 第 2 点:

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

...

When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. When applied to an operand that has array type, the result is the total number of bytes in the array) When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.

此外,在第 3.6 节中,第 3 点:

A byte is composed of a contiguous sequence of bits, the number of which is implementation-defined

关于c - sizeof 是否返回 C 中某种类型的字节数或八位字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11868211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com