C语言 uint8、uint16 和 uint32 的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22842707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
size of uint8, uint16 and uint32?
提问by user3458454
The code base uses data types like uint8[1 byte wide unsigned integer], uint16[2 byte wide unsigned integer], uint32[4 byte wide unsigned integer] etc.
代码库使用诸如 uint8[1 字节宽的无符号整数]、uint16[2 字节宽的无符号整数]、uint32[4 字节宽的无符号整数]等数据类型。
question: uint8 is same as char ?? uint16 is same as int ?? uint32 is same as long ?? uint64 is same as double ??
问题:uint8 和 char 一样??uint16 与 int 相同 ?? uint32 和 long 一样??uint64 与 double 相同 ??
for example : uint8 c[20]; // then the sizeof(c) should be 20- Isnt it ?? I wrote a small code in visual studio as shown below:
例如:uint8 c[20]; // 那么 sizeof(c) 应该是 20- 不是吗??我在visual studio中写了一段小代码,如下图:
#include <stdio.h>
#include <string.h>
typedef unsigned int uint32;
typedef unsigned int uint8;
int main()
{
double a = 1320.134;
uint32 b;
uint8 c[20];
b = (unsigned int)a;
c[3] = b;
printf("value is %d", c[3]);
return 1;
}
but the size of c in debug mode is 50. Why is it showing like that ??
但是调试模式下c的大小是50。为什么会这样显示??
回答by Keith Thompson
uint8, uint16, uint32, and uint64are probably Microsoft-specific types.
uint8、uint16、uint32和uint64可能是 Microsoft 特定的类型。
As of the 1999 standard, C supports standard typedefs with similar meanings, defined in <stdint.h>: uint8_t, uint16_t, uint32_t, and uint64_t. I'll assume that the Microsoft-specific types are defined similarly. Microsoft does support <stdint.h>, at least as of Visual Studio 2010, but older code may use uint8et al.
作为1999标准的,C支持标准的typedef具有类似含义,定义<stdint.h>:uint8_t,uint16_t,uint32_t,和uint64_t。我将假设 Microsoft 特定类型的定义类似。Microsoft 确实支持<stdint.h>,至少从 Visual Studio 2010 开始,但较旧的代码可能会使用uint8等。
The predefined types char, short, intet al have sizes that vary from one C implementation to another. The C standard has certain minimum requirements (charis at least8 bits, shortand intare at least 16, longis at least 32, and each type in that list is at least as wide as the previous type), but permits some flexibility. For example, I've seen systems where intis 16, 32, or 64 bits.
预定义类型char,short,int等人具有从一种C实施而变化的尺寸。C标准具有一定的最小要求(char是至少8位,short和int是至少16,long至少为32,并在该列表中的每个类型是至少一样宽的前型),但允许有一定的灵活性。例如,我见过int16、32 或 64 位的系统。
charis almostalways exactly 8 bits, but it's permitted to be wider. And plain charmay be either signed or unsigned.
char是几乎总是正好是8位,但它允许更宽。而plainchar可以是有符号的也可以是无符号的。
uint8_tis required to be an unsigned integer type that's exactly 8 bits wide. It's likely to be a typedef for unsigned char, though it might be a typedef for plain charif plain charhappens to be unsigned. If there is no predefined 8-bit unsigned type, then uint8_twill not be defined at all.
uint8_t要求是正好为 8 位宽的无符号整数类型。它很可能是 for 的 typedef unsigned char,但char如果 plainchar恰好是无符号的,它可能是 plain 的 typedef 。如果没有预定义的 8 位无符号类型,则uint8_t根本不会被定义。
Similarly, each uintN_ttype is an unsigned type that's exactly N bits wide.
同样,每种uintN_t类型都是无符号类型,正好是 N 位宽。
In addition, <stdint.h>defines corresponding signed intN_ttypes, as well as int_fastN_tand int_leastN_ttypes that are at leastthe specified width.
此外,<stdint.h>定义对应签名intN_t的类型,以及int_fastN_t与int_leastN_t那些类型的至少指定的宽度。
The [u]intN_ttypes are guaranteed to have no padding bits, so the size of each is exactly N bits. The signed intN_ttypes are required to use a 2's-complement representation.
这些[u]intN_t类型保证没有填充位,因此每个类型的大小正好是 N 位。签名intN_t类型需要使用 2 的补码表示。
Although uint32_tmight be the same as unsigned int, for example, you shouldn't assumethat. Use unsigned intwhen you need an unsigned integer type that's at least 16 bits wide, and that's the "natural" size for the current system. Use uint32_twhen you need an unsigned integer type that's exactly 32 bits wide.
例如,虽然uint32_t可能与 相同unsigned int,但您不应该假设。unsigned int当您需要一个至少 16 位宽的无符号整数类型时使用,这是当前系统的“自然”大小。uint32_t当您需要正好为 32 位宽的无符号整数类型时使用。
(And no, uint64or uint64_tis not the same as double; doubleis a floating-point type.)
(并且不,uint64或uint64_t与double; 不同,double是浮点类型。)
回答by unwind
It's quite unclear how you are computing the size ("the size in debug mode"?").
目前还不清楚您如何计算大小(“调试模式下的大小”?”)。
Use printf():
使用printf():
printf("the size of c is %u\n", (unsigned int) sizeof c);
Normally you'd print a size_tvalue (which is the type sizeofreturns) with %zu, but if you're using a pre-C99 compiler like Visual Studio that won't work.
通常,您会使用 打印一个size_t值(即sizeof返回的类型)%zu,但如果您使用的是像 Visual Studio 这样的 C99 之前的编译器,它将无法工作。
You need to find the typedefstatements in your code that define the custom names like uint8and so on; those are not standard so nobody here can know how they're defined in your code.
您需要typedef在代码中找到定义自定义名称的语句,例如uint8;这些不是标准的,所以这里没有人知道它们是如何在您的代码中定义的。
New C code should use <stdint.h>which gives you uint8_tand so on.
新的 C 代码应该使用<stdint.h>which 给你uint8_t等等。

