多头,多头多头,短暂多头。 。 。呵呵? [英] longs, long longs, short short long ints . . . huh?!

查看:66
本文介绍了多头,多头多头,短暂多头。 。 。呵呵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于long和int是什么或者编译器

依赖有什么标准吗?或者更依赖于架构?


我知道一个短的短整数并不是一个公认的类型,但是如果你想要确保它是16位,32位或64位等,你能单独依赖这些类型的名字吗?如果没有,有没有什么方法可以确保你想要一个64/32/16位int

$

干杯,
Dave。

解决方案

David Geering写道:


有没有任何关于long和int意味着什么的标准或者编译器是否依赖于b $ b?还是建筑依赖?



它依赖于编译器和体系结构。 C仅指定类型的最小值b / b $ b,而不是实际实现的大小。为符合C,int

需要至少为16位,长度必须至少为32位且长
需要至少为64位。编译器可以决定在任何平台上实现int == long == long long == 64位(这是现在常见的
看到32位整数8位平台)。


我知道一个短的短整数int实际上并不是一个公认的类型,但如果你想要b $ b想要确保它是16位,32位或64位等,你能单独依赖这些类型名称吗?如果没有,有什么办法可以确保你想要一个64/32/16位int




我通常使用以下两种方法之一:


如果数据不算算,我只做按位在

上的操作然后我将其视为字节数组。这不是100%可移植,因为我假设一个字节正好是8位(尽管这种方法

并不真正取决于假设,它是'使用

假设更容易。


如果数据是算术的,那么选择最大尺寸正确

代表数据和使用屏蔽来截断数据。


想要确切位长数据类型的真正问题是当你想要用外部程序传递数据时通过

传输或保存到文件。有很多线程

讨论这个问题,但通常在字节序的标题下和

可移植性。但这个建议在这里仍然有用。使用移位运算符一次读/写
字节的数据。这允许你丢弃/忽略

变量中的额外位如果碰巧有比你想要的b / b更多的位数。


David Geering写道:


是否有任何关于long和int意思的标准或是编译器

依赖?还是建筑依赖?



它们的确切大小和范围是特定于实现的。应用

sizeof运算符来获取字节大小(一个字节不一定是C中的
八位字节),并使用limits.h中定义的宏和浮点数.h找到

超出范围。


该标准确实规定了符合实施要求的所有符合条件的某些最低要求。请参阅以下链接中的C99

草案的5.2.4.2节:

< http://people.freebsd.org/~green/c9x-draft.txt> ;


我知道一个短的短整数并不是一个公认的类型,但是如果你想要确定它是b $ b 16位,32位或64位等,你能单独依赖这些类型的名字吗?如果没有,有什么办法可以确保你想要一个64/32/16位int




C99为这些目的定义了一堆新类型。它们是stdint.h中定义的
和用于执行I / O的一些相关格式说明符

在inttypes.h中。例如,如果您需要一个大小为
的类型正好是32位,请使用int32_t或uint32_t。


在系统上查找这些标准头文件。


另请查看上面链接的草案的第7.18和7.8节。


santosh写道:


David Geering写道:



.... snip ...


>


>我知道一个短的短整数int实际上并不是一个公认的类型,
但如果你想确保它是16位,32位或64位
等,您能单独依赖这些类型名称吗?如果没有,是否有任何方法可以确保在需要时获得64/32/16位int?



C99为这些目的定义了一堆新类型。它们是在stdint.h中定义的
和一些关联的格式说明符

与它们进行I / O是在inttypes.h中。例如,如果您需要一个大小正好为32位的

类型,请使用int32_t或uint32_t。


在系统上查找这些标准头文件。



然后避免使用它们。它们不是必需的,结果

是不可移植的代码。


-

Chuck F(cinefalconer at maineline dot net)

适用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>


Is there any standard as to what a long and int mean or is it compiler
dependent? Or furthermore architecture dependent?

I know a short short long int isn''t actually a recognised type, but if you
wanted to make sure it was 16-bit, 32-bit or 64-bit etc, can you rely on
these type names alone? If not, is there any way to ensure you get a
64/32/16-bit int when you want one?

Cheers,
Dave.

解决方案

David Geering wrote:

Is there any standard as to what a long and int mean or is it compiler
dependent? Or furthermore architecture dependent?

It''s compiler and architecture dependent. C only specifies the minimum
size of a type, not it''s actual implemented size. To comply with C, int
needs to be at least 16 bits, long needs to be at least 32 bits and
long long needs to be at least 64 bits. A compiler may decide to
implement int == long == long long == 64 bits on any platform (It''s
common nowdays to see 32 bit ints on 8 bit platforms).

I know a short short long int isn''t actually a recognised type, but if you
wanted to make sure it was 16-bit, 32-bit or 64-bit etc, can you rely on
these type names alone? If not, is there any way to ensure you get a
64/32/16-bit int when you want one?

I usually use one of 2 methods:

If the data is not arithmetic and I''m only doing bitwise operations on
it then I treat it as a byte array. This is not 100% portable since I
make the assumption that a byte is exactly 8 bits (though this method
does not really depend on the assumption, it''s easier with the
assumption).

If the data is arithmetic then choose the largest size to properly
represent the data and use masking to truncate the data.

The real issue of wanting an exact bitlength of a datatype is when you
want to communicate the data with an external program either via
transmitting it or saving it to file. There are lots of threads
discussing this issue but usually under the heading of endianness and
portability. But the advice is still useful here. Read/write the data a
byte at a time using shift operators. This allows you to discard/ignore
the extra bits in the variable if it happens to have more bits than you
intended.


David Geering wrote:

Is there any standard as to what a long and int mean or is it compiler
dependent? Or furthermore architecture dependent?

Their exact size and range are implementation specific. Apply the
sizeof operator to get the size in bytes, (a byte is not necessarily an
octet in C), and use the macros defined in limits.h and float.h to find
out the range.

The standard does specify certain minimum requirements that all
conforming implementations must meet. See section 5.2.4.2 of the C99
draft at the link below:
<http://people.freebsd.org/~green/c9x-draft.txt>

I know a short short long int isn''t actually a recognised type, but if you
wanted to make sure it was 16-bit, 32-bit or 64-bit etc, can you rely on
these type names alone? If not, is there any way to ensure you get a
64/32/16-bit int when you want one?

C99 has defined a bunch of new types for these purposes. They''re
defined in stdint.h and some associated format specifiers for doing I/O
with them are in inttypes.h. For example if you need a type whose size
is exactly 32 bits, use int32_t or uint32_t.

Look up these standard header files on your system.

Also look up section 7.18 and 7.8 of the draft linked above.


santosh wrote:

David Geering wrote:

.... snip ...

>

>I know a short short long int isn''t actually a recognised type,
but if you wanted to make sure it was 16-bit, 32-bit or 64-bit
etc, can you rely on these type names alone? If not, is there
any way to ensure you get a 64/32/16-bit int when you want one?


C99 has defined a bunch of new types for these purposes. They''re
defined in stdint.h and some associated format specifiers for
doing I/O with them are in inttypes.h. For example if you need a
type whose size is exactly 32 bits, use int32_t or uint32_t.

Look up these standard header files on your system.

And then avoid using them. They are not required, and the result
is non-portable code.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


这篇关于多头,多头多头,短暂多头。 。 。呵呵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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