跨平臺C/C++資料型別定義
1.1 編譯器執行作業系統
l WINDOWS : _WIN32、WIN32;
l UNIX/LINUX: unix、__unix、__unix__;
l SunOS/SOLARIS: __SVR4、__svr4__、sun、__sun、__sun__、sparc、__sparc、__sparc__;
l HPUX: __hppa、__hppa__、__hpux、__hpux__、_HPUX_SOURCE;
l AIX: _AIX、_AIX32、_AIX41、_AIX43、_AIX51、_AIX52;
l LINUX: linux、__linux、__linux__、__gnu_linux__;
l CPU: __x86_64、__x86_64__(Intel); __amd64、__amd64__(AMD); sparc、 __sparc、__sparc__;
1.2 編譯器資訊
l __STDC__: ANSI C標誌,值為1,說明相容ANSI C標準;
l __GNUC__: GCC編譯器編預定義巨集;__GNUC__的值表示GCC版本號;
l __GNUC_MINOR__: GCC次版本號;
l __GNUC_PATCHLEVEL__: GCC編譯器修訂版本號,l
l __GLIBC_MINOR__: glib的次版本號;
l VC++的版本巨集:
l Visual Studio 5.0 --> VC++5.0 _MSC_VER = 1100
l Visual Studio 6.0 --> VC++6.0 _MSC_VER = 1200
l Visual Studio 2002 --> VC++7.0 _MSC_VER = 1300
l Visual Studio 2003 --> VC++7.1 _MSC_VER = 1310
l Visual Studio 2005 --> VC++8.0 _MSC_VER = 1400
l Visual Studio 2008 --> VC++9.0 _MSC_VER = 1500
1.3 ISO C 標準
1.3.1 <limits.h>
char,int,short int,long,long long 限制性說明標頭檔案。<limits.h>定義了char,int,short,long,long long 最大值最小值等。如表一所示。
表一 <limits.h>定義
Name |
Description |
Typical value |
||
CHAR_BIT |
Number of bits in a char |
8 |
≥+8 |
|
SCHAR_MIN |
Minimum value for a signed char |
–128 |
≤–127 |
|
SCHAR_MAX |
Maximum value for a signed char |
+127 |
≥+127 |
|
UCHAR_MAX |
Maximum value for an unsigned char |
≥+255 |
||
CHAR_MIN |
Minimum value for a char |
–128 |
≤–127 (if char is represented as a signed char; otherwise ≤0) |
|
CHAR_MAX |
Maximum value for a char |
+127 |
≥+127 |
|
MB_LEN_MAX |
Maximum multi byte length of a character across all locales |
varies, usually at least 4 |
≥+1 |
|
SHRT_MIN |
Minimum value for a short int |
–32,768 |
≤–32,767 |
|
SHRT_MAX |
Maximum value for a short int |
+32,767 |
≥+32,767 |
|
USHRT_MAX |
Maximum value for an unsigned short int |
+65,535 |
≥+65,535 |
|
INT_MIN |
Minimum value for an int |
–2,147,483,648 |
≤–32,767 |
|
INT_MAX |
Maximum value for an int |
+2,147,483,647 |
≥+32,767 |
|
UINT_MAX |
Maximum value for anunsigned int |
+4,294,967,295 |
≥+65,535 |
|
LONG_MIN |
Minimum value for a long int |
32 bit compiler |
–2,147,483,648 |
≤–2,147,483,647 |
64 bit compiler |
–9,223,372,036,854,775,808 |
|||
LONG_MAX |
Maximum value for a long int |
32 bit compiler |
+2,147,483,647 |
≥+2,147,483,647 |
64 bit compiler |
+9,223,372,036,854,775,807 |
|||
ULONG_MAX |
Maximum value for an unsigned long int |
32 bit compiler |
+4,294,967,295 |
≥+4,294,967,295 |
64 bit compiler |
+18,446,744,073,709,551,615 |
|||
LLONG_MIN |
Minimum value for a long long int |
–9,223,372,036,854,775,808 |
≤-9,223,372,036,854,775,807 |
|
LLONG_MAX |
Maximum value for a long long int |
+9,223,372,036,854,775,807 |
≥+9,223,372,036,854,775,807 |
|
ULLONG_MAX |
Maximum value for an unsigned long long int |
+18,446,744,073,709,551,615 |
≥+18,446,744,073,709,551,615 |
1.3.2 常用型別說明
l char:typical 8-bits,Many DSP may define a char-bits equal to 16 bits or more.
l short:typical 16-bits
l int:typical 16-bits or 32-bits, bits-number of int must be more than short.
l long:typical 32-bits,also larger than int
l long long:typical 64-bits
2 資料型別定義
//////////////////////////////////////////////////////////////////////////
// TOD: filename: types.h
// 1.Using this head file for data-type defining,
// Firstly make sure the type is defined
// 2.Example
// 1.Define a 8-bits variable : INT8 chVariable;
// 2.Define a 16-bits variable: INT16 snVariable;
// 3.Define a 32-bits variable: INT32 nVariable;
// 4.Define a 64-bits variable: INT64 llVariable;
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//C Standards
//////////////////////////////////////////////////////////////////////////
#if defined(__STDC__)
# define PREDEF_STANDARD_C_1989
# if defined(__STDC_VERSION__)
# define PREDEF_STANDARD_C_1990
# if (__STDC_VERSION__ >= 199409L)
# define PREDEF_STANDARD_C_1994
# endif
# if (__STDC_VERSION__ >= 199901L)
# define PREDEF_STANDARD_C_1999
# endif
# endif
#endif
//////////////////////////////////////////////////////////////////////////
// Pre-C89 compilers do not recognize certain keywords.
// Let the preprocessor remove those keywords for those compilers.
//////////////////////////////////////////////////////////////////////////
#if !defined(PREDEF_STANDARD_C_1989) && !defined(__cplusplus)
# define const
# define volatile
#endif
//////////////////////////////////////////////////////////////////////////
// Define 8-bits Integer, 16-bits Integer,32-bits Integer
// All compliant compilers that support Standard C/C++
// VC++, Borland C++, Turb C++ those who support C89,but undefined __STDC__)
//////////////////////////////////////////////////////////////////////////
#if defined(__STDC__) || defined(__cplusplus) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__TURBOC__)
#include <limits.h>
// Defined 8 - bit Integer
#if defined(UCHAR_MAX) && (UCHAR_MAX == 0xFF)
typedef char INT8, *INT8_PTR;
typedef unsigned char UINT8, *INT8_PTR;
#ifndef DEFINED_INT8
#define DEFINED_INT8
#endif
#endif
// Defined 16-bits Integer
#if defined(USHRT_MAX) && (USHRT_MAX == 0xFFFF)
typedef short int INT16, *INT16_PTR;
typedef unsigned short int UINT16, *UINT16_PTR;
#ifndef DEFINED_INT16
#define DEFINED_INT16
#endif
#elif defined(UINT_MAX) && (UINT_MAX == 0xFFFF)
typedef int INT16, *INT16_PTR;
typedef unsigned int UINT16, *UINT16_PTR;
#ifndef DEFINED_INT16
#define DEFINED_INT16
#endif
#endif
// Defined 32-bits Integer
#if defined(UINT_MAX) && (UINT_MAX == 0xFFFFFFFFUL)
typedef int INT32, *INT32_PTR;
typedef unsigned int UINT32, *UINT32_PTR;
#ifndef DEFINED_INT32
#define DEFINED_INT32
#endif
#elif defined(ULONG_MAX) && (ULONG_MAX == 0xFFFFFFFFUL)
typedef long int INT32, *INT32_PTR;
typedef unsigned long int UINT32, *UINT32_PTR;
#ifndef DEFINED_INT32
#define DEFINED_INT32
#endif
#endif
#endif
//////////////////////////////////////////////////////////////////////////
// Define 64-bits Integer
// Here Only support typical systems
// such as GNU/Linux Windows UNIX Vxworks BSD Solaris
//////////////////////////////////////////////////////////////////////////
// GNU/Linux System 64-bits Integer
#if defined(__GNUC__) || defined(linux) ||defined(__linux)
#if defined (__GLIBC_HAVE_LONG_LONG) || (defined(ULLONG_MAX) && (ULLONG_MAX == 0xFFFFFFFFFFFFFFFFUL))
typedef long long INT64, *INT64_PTR;
typedef unsigned long long UINT64, *INT64_PTR;
#ifndef DEFINE_INT64
#define DEFINE_INT64
#endif
#endif
#endif
// Windows System 64-bits Integer
#if defined (WIN32) || defined (_WIN32)
#if defined(_MSC_VER) || defined(__BORLANDC__)
typedef __int64 INT64, *INT64_PTR;
typedef unsigned __int64 UINT64, *INT64_PTR;
#ifndef DEFINE_INT64
#define DEFINE_INT64
#endif
#else
typedef unsigned long long INT64, *INT64_PTR;
typedef signed long long UINT64, *INT64_PTR;
#ifndef DEFINE_INT64
#define DEFINE_INT64
#endif
#endif
#endif
// UNIX
#if defined(unix) || defined(__unix__) || defined(__unix)
# define PREDEF_PLATFORM_UNIX
#endif
#if defined(PREDEF_PLATFORM_UNIX)
#include <unistd.h>
#if defined(_XOPEN_VERSION)
#if (_XOPEN_VERSION >= 3)
#define PREDEF_STANDARD_XOPEN_1989
#endif
#if (_XOPEN_VERSION >= 4)
#define PREDEF_STANDARD_XOPEN_1992
#endif
#if (_XOPEN_VERSION >= 4) && defined(_XOPEN_UNIX)
#define PREDEF_STANDARD_XOPEN_1995
#endif
#if (_XOPEN_VERSION >= 500)
#define PREDEF_STANDARD_XOPEN_1998
#endif
#if (_XOPEN_VERSION >= 600)
#define PREDEF_STANDARD_XOPEN_2003
typedef unsigned long long INT64, *INT64_PTR;
typedef signed long long UINT64, *INT64_PTR;
#ifndef DEFINE_INT64
#define DEFINE_INT64
#endif
#endif
# endif
#endif