本文介绍了sizeof是否返回C中某个类型的字节数或八位字节数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需放入C和变体(不同于在其虚拟机中使用Java的样子),不同目标上的基本类型的大小可以相差很大,并且除非您使用在<$ c $中定义的固定宽度类型,否则实际上不能保证。 c> stdint.h ,即使这样您的实现也必须支持它们。

Simply put in C and variants (unlike that wuss java with its virtual machine) the size of primitive types on different targets can vary greatly, and there is really no guarantee unless you use the fixed width types defined in stdint.h, and even then your implemenation has to support them.

无论如何,假设(因为在大多数现代计算机上,字节是一个字节八位字节,出于联网目的,我假设sizeof是否以字节或八位字节返回数据类型的大小?

Anyway hypothetically(because on most modern machines a byte is an octet, for networking purposes I assume(ASCII)) does sizeof return the size of a datatype in bytes or in octets?

推荐答案

答案: sizeof 返回类型的大小,以字节为单位。

Answer: sizeof returns the size of the type in bytes.

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

Example: sizeof(char) is 100% guaranteed to be 1, but this does not mean, that it's one octet (8 bits).

由标准证明:

在6.5.3.4中,点2:

in 6.5.3.4, point 2:

...

应用于具有char,unsigned char或signed char,
(或其限定版本)类型的操作数时,结果为1
。当应用于具有数组
类型的操作数时,结果为数组中的字节总数)当应用于具有结构或联合类型的操作数
时,结果为字节总数在这样的对象中,
包括内部填充和结尾填充。

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点:

Also, in Section 3.6, point 3:

这篇关于sizeof是否返回C中某个类型的字节数或八位字节数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:29