int64_t数据类型在C中 [英] int64_t DATA TYPE IN C

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

问题描述

C中的 int64_t long 数据类型有什么区别吗? int64_t 是否减少了代码的可移植性?

解决方案

看看: C数据类型 [ ^ ]



如你所见,这一切都取决于平台和编译器,但你可以依靠sizeof(long)来返回正确的给定平台的字节数,你可以做自己的typedef,如:

 #ifdef _MSC_VER 
typedef unsigned char uchar;
typedef unsigned char byte;
typedef char sbyte;
typedef short int16;
typedef unsigned short uint16;
typedef unsigned short ushort;
typedef int int32;
typedef unsigned int uint32;
typedef unsigned int uint;

typedef long long int64;
typedef unsigned long long uint64;

#endif





祝你好运

Espen Harlinn


一些基本数据类型的大小受实际编译器使用的数据模型的影响。对于不同的编译器,整数类型的大小可能不同。有关编译器使用的数据模型列表(LP64,ILP64,LP32,ILP32,...),请查看以下站点:

http://www.unix.org/version2/whatsnew/lp64_wp.html [ ^ ]

http://msdn.microsoft.com/en-us/library/windows/desktop/aa384083%28v=vs.85%29.aspx [ ^ ]



我通常使用以下技术定义自己的固定大小数据类型(int8 - int64和uint8 - uint64): />

  #ifdef _MSC_VER 

/ / 使用特定于Visual C ++的__intX类型
typedef signed __ int8 int8;
typedef unsigned __ int8 uint8 ;
typedef 签名 __ int16 int16 ;
...

#else

# include < stdint.h >
typedef int8_t int8;
typedef uint8_t uint8;
typedef int16_t int16;
...

#endif





较新的Visual C ++版本有< stdint.h>包括像大多数unix系统一样。它是一个包含int8_t及其朋友的标准头,所以它非常便携。

另一个小细节是 char signed char unsigned char 是3种不同的类型! char signed char 与许多人认为不一样,但在99%的情况下都进行了自动演员。作为证明,您可以编写3个重载函数,接受这3种不同类型!


某些系统上的long是32位(与整数相同),int64_t定义为64位整数在所有系统上(也称为long long)。



使用long可能会影响可移植性,但使用int64_t看起来像是为了提高可移植性而创建的。

Is there any difference between int64_t and long data types in C? Does int64_t reduce portability of the code ?

解决方案

Have a look at: C data types[^]

As you see, it all depends on the platform and the compiler, but you can rely on sizeof(long) to return the correct number of bytes for a given platform, and you can do your own typedefs, like:

#ifdef _MSC_VER
typedef unsigned char uchar;
typedef unsigned char byte;
typedef char sbyte;
typedef short int16;
typedef unsigned short uint16;
typedef unsigned short ushort;
typedef int int32;
typedef unsigned int uint32;
typedef unsigned int uint;

typedef long long int64;
typedef unsigned long long uint64;

#endif



Best regards
Espen Harlinn


The size of some basic data types is affected by the data model used by your actual compiler. With different compilers the size of your integral types may differ. For a list of data models used by compilers (LP64, ILP64, LP32, ILP32, ...) check out these sites:
http://www.unix.org/version2/whatsnew/lp64_wp.html[^]
http://msdn.microsoft.com/en-us/library/windows/desktop/aa384083%28v=vs.85%29.aspx[^]

I usually define my own fixed size data types (int8 - int64 and uint8 - uint64) with the following technique:

#ifdef _MSC_VER

// using Visual C++ specific __intX types
typedef signed __int8 int8;
typedef unsigned __int8 uint8;
typedef signed __int16 int16;
...

#else

#include <stdint.h>
typedef int8_t int8;
typedef uint8_t uint8;
typedef int16_t int16;
...

#endif



Newer Visual C++ versions have an <stdint.h> include like most unix systems. It is a standard header containing int8_t and its friends so its very well portable.
Another tiny detail is that char, signed char, and unsigned char are 3 different types! char and signed char are not the same as many think but automatic cast is done in 99% of the cases. As a proof you can write 3 overloaded functions accepting these 3 different types!


A long on some systems is 32 bits (same as an integer), the int64_t is defined as a 64 bit integer on all systems (otherwise known as a long long).

Portability may be affected using long, but using int64_t looks like it was created to increase portability.


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

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