在C中定义的16位整数 [英] Define 16 bit integer in C

查看:708
本文介绍了在C中定义的16位整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要声明的16位大小的整数,在C

I need to declare an integer in the size of 16 bit, in C.

我知道短期和int大小与计算机有关。

I know that short and int sizes are machine dependent.

我试图用stdint.h,但似乎他们根本就

I tried to use "stdint.h", but it seems that they simply do

typedef short int16_t

所以我的问题是:

So my question is:

我缺少的东西,短型保证16位长?

Am I missing something and short type guarantees 16 bit length?

如果没有,是否有是保证它的方法吗?

If no, is there is an alternative that guarantees it?

推荐答案

这意味着 int16_t 定义为上的的机器,而不是所有的机器。

That means int16_t is defined as short on your machine, not all machines.

只需使用 int16_t ,你绝对需要一个16位的整数类型;它被定义为适合在提供所有平台 stdint.h (这应该是所有支持C99,或 cstdint 对于C ++)。

Just use the int16_t where you absolutely need a 16bit integer type; it will be defined as appropriate on all platforms that provide stdint.h (which should be all that support C99, or cstdint for C++).

:要澄清,在 stdint.h 头文件是由C(或C ++)编译器提供的,因此它的内容将每编译器版本,系统,CPU架构,等等。这很可能各不相同,编译器套件的作者知道到底是什么类型有哪些系统尺寸是什么。看着这个文件只是一个系统上只告诉你一个特定的编译器的一个特定的操作系统特定版本上特定的体系结构(如GCC 4.2达尔文x86_64的,或Visual Studio在WinNT阿尔法,或ICC在Solaris IA32的定义等)。一些系统,尤其是嵌入式的人,可能有不同类型的大小,所以一个可能并不总是16位,编译器会知道使用该位长度合适的尺寸

To clarify, the "stdint.h" header file is provided by the C (or C++) compiler, so its contents will likely vary per compiler, version, system, CPU architecture, etc. That is, the authors of the compiler suite know exactly what types have what sizes on which systems. Looking at that file on just one system only tells you about the definitions for a particular version of a particular compiler on a particular OS on a particular architecture (e.g. GCC 4.2 on Darwin x86_64, or Visual Studio on WinNT Alpha, or ICC on Solaris IA32, etc). Some systems, especially embedded ones, might have different type sizes, so a short might not always be 16 bits, and the compiler would know the right size to use for that bit length.

如果你看一下文件 stdint.h 另一个系统上的定义可能不同,也可能是相同的 - 但它的目的是为定义为整型保证比特长度。

If you look at the file stdint.h on another system the definitions might be different, or they might be the same - but its purpose is to provide the definitions for integer types of guaranteed bit lengths.

这篇关于在C中定义的16位整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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