簡體   English   中英

C ++中的int和long之間有什么區別?

[英]What is the difference between an int and a long in C++?

如果我錯了請糾正我,

int為4個字節,取值范圍為-2,147,483,648至2,147,483,647(2 ^ 31)
long是4個字節,取值范圍為-2,147,483,648到2,147,483,647(2 ^ 31)

C ++有什么區別? 它們可以互換使用嗎?

它取決於實現。

例如,在Windows下它們是相同的,但是例如在Alpha系統上,long是64位,而int是32位。 本文介紹了可變平台上的Intel C ++編譯器的規則。 總結一下:

  OS           arch           size
Windows       IA-32        4 bytes
Windows       Intel 64     4 bytes
Windows       IA-64        4 bytes
Linux         IA-32        4 bytes
Linux         Intel 64     8 bytes
Linux         IA-64        8 bytes
Mac OS X      IA-32        4 bytes
Mac OS X      Intel 64     8 bytes  

您唯一的保證是:

sizeof(char) == 1
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

// FROM @KTC. The C++ standard also has:
sizeof(signed char)   == 1
sizeof(unsigned char) == 1

// NOTE: These size are not specified explicitly in the standard.
//       They are implied by the minimum/maximum values that MUST be supported
//       for the type. These limits are defined in limits.h
sizeof(short)     * CHAR_BIT >= 16
sizeof(int)       * CHAR_BIT >= 16
sizeof(long)      * CHAR_BIT >= 32
sizeof(long long) * CHAR_BIT >= 64
CHAR_BIT         >= 8   // Number of bits in a byte

參見: long保證至少有32位?

當為x64進行編譯時,int和long之間的差異在0到4個字節之間,具體取決於您使用的編譯器。

GCC使用LP64模型,這意味着在64位模式下,整數是32位,而long是64位。

例如,MSVC使用LLP64模型,這意味着即使在64位模式下,int和long均為32位。

C ++規范本身 (舊版本,但對此已經足夠好了)使此問題懸而未決。

有四種有符號整數類型:' signed char ',' short int ',' int '和' long int '。 在此列表中,每種類型提供的存儲量至少與列表中位於其前面的類型相同。 普通整數具有執行環境的體系結構建議的自然大小*;

[腳注:即,它足夠大以包含標題<climits>定義的INT_MIN和INT_MAX范圍內的任何值。 ---結束foonote]

正如Kevin Haines指出的那樣,int具有執行環境建議的自然大小,必須適合INT_MIN和INT_MAX。

C89標准規定UINT_MAX應該至少為2 ^ 16-1, USHRT_MAX 2 ^ 16-1和ULONG_MAX 2 ^ 32-1。 這使得short和int的位數至少為16,而long的位數至少為32。 對於char,它明確指出它應至少具有8位( CHAR_BIT )。 C ++繼承了limits.h文件的這些規則,因此在C ++中,我們對這些值具有相同的基本要求。 但是,您應該得到從這個int至少為2個字節。 從理論上講,char,int和long都可以是1個字節,在這種情況下CHAR_BIT必須至少為32。請記住,“ byte”始終是char的大小,因此,如果char較大,則字節不僅是8位還有。

這取決於您的編譯器。 您可以保證long至少與int一樣大,但是不能保證它會更長。

在大多數情況下,字節數和值的范圍由CPU的體系結構而不是C ++決定。 但是,C ++設置了最低要求,而litb對此進行了正確解釋,而Martin York僅犯了一些錯誤。

您不能使用int和long互換的原因是因為它們的長度並不總是相同。 C是在PDP-11上發明的,其中一個字節有8位,int是兩個字節,可以直接由硬件指令處理。 由於C程序員經常需要四字節算術,因此發明了long,它是四字節,由庫函數處理。 其他機器具有不同的規格。 C標准提出了一些最低要求。

如果您曾經在另一台計算機體系結構,OS或另一家供應商的編譯器上編譯代碼,那么依靠編譯器供應商對基本類型大小的實現會困擾您。

大多數編譯器供應商都提供一個頭文件,該頭文件定義具有顯式類型大小的基本類型。 當有可能將代碼移植到另一個編譯器時,應使用這些原始類型(在每個實例中始終將其讀取)。 例如,大多數UNIX編譯器都具有int8_t uint8_t int16_t int32_t uint32_t Microsoft具有INT8 UINT8 INT16 UINT16 INT32 UINT32 我更喜歡Borland / CodeGear的int8 uint8 int16 uint16 int32 uint32 這些名稱還使您想起了預期值的大小/范圍。

多年以來,我一直使用Borland的顯式基元類型名稱並#include以下C / C ++頭文件(primitive.h),該文件旨在為任何C / C ++編譯器使用這些名稱定義顯式基元類型(此頭文件可能實際上並不涵蓋了所有編譯器,但涵蓋了我在Windows,UNIX和Linux上使用過的幾個編譯器,它也(尚未)定義64位類型)。

#ifndef primitiveH
#define primitiveH
// Header file primitive.h
// Primitive types
// For C and/or C++
// This header file is intended to define a set of primitive types
// that will always be the same number bytes on any operating operating systems
// and/or for several popular C/C++ compiler vendors.
// Currently the type definitions cover:
// Windows (16 or 32 bit)
// Linux
// UNIX (HP/US, Solaris)
// And the following compiler vendors
// Microsoft, Borland/Imprise/CodeGear, SunStudio,  HP/UX
// (maybe GNU C/C++)
// This does not currently include 64bit primitives.
#define float64 double
#define float32 float
// Some old C++ compilers didn't have bool type
// If your compiler does not have bool then add   emulate_bool
// to your command line -D option or defined macros.
#ifdef emulate_bool
#   ifdef TVISION
#     define bool int
#     define true 1
#     define false 0
#   else
#     ifdef __BCPLUSPLUS__
      //BC++ bool type not available until 5.0
#        define BI_NO_BOOL
#        include <classlib/defs.h>
#     else
#        define bool int
#        define true 1
#        define false 0
#     endif
#  endif
#endif
#ifdef __BCPLUSPLUS__
#  include <systypes.h>
#else
#  ifdef unix
#     ifdef hpux
#        include <sys/_inttypes.h>
#     endif
#     ifdef sun
#        include <sys/int_types.h>
#     endif
#     ifdef linux
#        include <idna.h>
#     endif
#     define int8 int8_t
#     define uint8 uint8_t
#     define int16 int16_t
#     define int32 int32_t
#     define uint16 uint16_t
#     define uint32 uint32_t
#  else
#     ifdef  _MSC_VER
#        include <BaseTSD.h>
#        define int8 INT8
#        define uint8 UINT8
#        define int16 INT16
#        define int32 INT32
#        define uint16 UINT16
#        define uint32 UINT32
#     else
#        ifndef OWL6
//          OWL version 6 already defines these types
#           define int8 char
#           define uint8 unsigned char
#           ifdef __WIN32_
#              define int16 short int
#              define int32 long
#              define uint16 unsigned short int
#              define uint32 unsigned long
#           else
#              define int16 int
#              define int32 long
#              define uint16 unsigned int
#              define uint32 unsigned long
#           endif
#        endif
#      endif
#  endif
#endif
typedef int8   sint8;
typedef int16  sint16;
typedef int32  sint32;
typedef uint8  nat8;
typedef uint16 nat16;
typedef uint32 nat32;
typedef const char * cASCIIz;    // constant null terminated char array
typedef char *       ASCIIz;     // null terminated char array
#endif
//primitive.h

C ++標准這樣說:

3.9.1,§2:

有五種有符號整數類型:“有符號字符”,“ short int”,“ int”,“ long int”和“ long long int”。 在此列表中,每種類型提供的存儲量至少與列表中位於其前面的類型相同。 普通整數具有執行環境的架構所建議的自然大小(44); 提供其他有符號整數類型以滿足特殊需要。

(44),即足夠大以包含標頭<climits>定義的INT_MIN和INT_MAX范圍內的任何值

結論:這取決於您正在使用的體系結構。 任何其他假設都是錯誤的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM