C语言 C 中基本数据类型的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14821738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:20:14  来源:igfitidea点击:

size of basic data types in C

ctypes

提问by liv2hak

I have a sample program that I copied from some website.

我有一个从某个网站复制的示例程序。

int main(void)
{
   int answer;
   short x = 1;
   long y = 2;
   float u = 3.0;
   double v = 4.4;
   long double w = 5.54;
   char c = 'p';

   typedef enum
   {
      kAttributeInvalid,
      kBooleanAttributeActive,
      kBooleanAttributeAlarmSignal,
      kBooleanAttributeAlign64,
      kBooleanAttributeAutoNegotiationComplete,
   }codes_t;

  /* __DATE__, __TIME__, __FILE__, __LINE__ are predefined symbols */
  #if 0
  printf("Date : %s\n", __DATE__);
  printf("Time : %s\n", __TIME__);
  printf("File : %s\n", __FILE__);
  printf("Line : %d\n", __LINE__);
  #endif

  /* The size of various types */
  printf("The size of int         %zu\n", sizeof(answer));
  printf("The size of short       %zu\n", sizeof(x));
  printf("The size of long        %zu\n", sizeof(y));
  printf("The size of float       %zu\n", sizeof(u));
  printf("The size of double      %zu\n", sizeof(v));
  printf("The size of long double %zu\n", sizeof(w));
  printf("The size of char        %zu\n", sizeof(c));
  printf("The size of enum        %zu\n", sizeof(codes_t));

  return 0;
}

I ran this program and the output that I got is as follows.

我运行了这个程序,得到的输出如下。

The size of int         4
The size of short       2
The size of long        8
The size of float       4
The size of double      8
The size of long double 16
The size of char        1
The size of enum        4

I am running this on a linux PC that is running 64-bit Ubuntu.My question is if I were to run the same program on a 32-bit machine will I see different results.Or in other words does the size of the basic data types depend on

我在运行 64 位 Ubuntu 的 linux PC 上运行它。我的问题是如果我要在 32 位机器上运行相同的程序,我会看到不同的结果。或者换句话说,基本数据的大小类型取决于

  1. processor
  2. Operating System
  3. anything else
  1. 处理器
  2. 操作系统
  3. 还要别的吗

采纳答案by Mats Petersson

Subject to having to install some libraries [probably just glibc] in it's 32-bit variant, you should be able to try this yourself by using gcc -m32 myprog.c[or clang -m32 myprog.c].

由于必须在 32 位变体中安装一些库 [可能只是 glibc],您应该可以使用gcc -m32 myprog.c[或clang -m32 myprog.c]自己尝试一下。

However, the only thing of your items that have been listed that will change if you move from a 64-bit x86 linux system to 32-bit x86 linux system, using gcc-based compilers, is the size of long. Note the heavy qualification of x86, gcc, etc - compilers have a lot of freedom. Someone could write a compiler for Linux that uses 16-bit intand 64-bit longon a 32-bit system with no huge amount of difficulty. Using that compiler to compile the Linux kernel and many of the Linux tools would probably fail [most likely including compiling gccwith that compiler]. But you can't really say "on this architecture" or "in this OS" or "with this compiler" ... without also qualifying what the OTHER parameters are.

但是,如果您使用基于 gcc 的编译器从 64 位 x86 linux 系统移动到 32 位 x86 linux 系统,那么列出的项目中唯一会改变的就是long. 请注意 x86、gcc 等的重限定——编译器有很大的自由度。有人可以为 Linux 编写一个编译器,在 32 位系统上使用 16 位int和 64long位,没有太大的困难。使用该编译器编译 Linux 内核和许多 Linux 工具可能会失败[很可能包括gcc使用该编译器进行编译]。但是你不能真正说“在这个架构上”或“在这个操作系统中”或“使用这个编译器”......而不限定 OTHER 参数是什么。

Case in point: A Microsoft C/C++ compiler has a longthat is 32 bit even on 64-bit systems. Why, I hear you ask? Because a large number of Windows API functions use longas a 32-bit value as legacy from when Windows was a 16-bit OS on Intel 286/386 processors. Since (some of) the system calls are backwards compatible a very long way in Windows, code that is written for 16-bit systems will still work on 64-bit Windows [unless the code is using some really unusual system calls, and of course, the STYLE will look a bit ancient]. Changing longto a 64-bit value would have broken some of that functioanilty, so the compiler guys at MS decided to stick with long= 32 bit. If you want 64-bit integers, you have to use long longor int64_tor something else, not long. Of course, this breaks some code that assumes that sizeof(long) == sizeof(void *). Hopefully, most such code has already been fixed...

举个例子:long即使在 64 位系统上,Microsoft C/C++ 编译器也具有32 位。为什么,我听到你问?因为long当 Windows 是 Intel 286/386 处理器上的 16 位操作系统时,大量 Windows API 函数用作 32 位值。由于(某些)系统调用在 Windows 中向后兼容很长一段时间,为 16 位系统编写的代码仍然可以在 64 位 Windows 上运行 [除非代码使用了一些非常不寻常的系统调用,当然,STYLE 会显得有点古老]。更改long为 64 位值会破坏某些功能,因此 MS 的编译器人员决定坚持使用long= 32 位。如果你想要 64 位整数,你必须使用long longint64_t或其他东西,而不是long. 当然,这会破坏一些假设sizeof(long) == sizeof(void *). 希望大多数这样的代码已经被修复......

回答by

My question is if I were to run the same program on a 32-bit machine will I see different results

我的问题是如果我要在 32 位机器上运行相同的程序,我会看到不同的结果吗

Maybe. Or maybe not.

也许。或者可能不是。

Or in other words does the size of the basic data types depend on 1) processor 2) Operating System 3) anything else

或者换句话说,基本数据类型的大小是否取决于 1) 处理器 2) 操作系统 3) 其他任何东西

  1. Yes, 2. yes, 3. yes, for example if you run a 32-bit app in 32-bit compatibility mode on a 64-bit OS, then it most likely will use a 32-bit word size (of course, it was compiled like that). Oh, and yes, it may depend on the compiler too.
  1. 是的,2. 是的,3. 是的,例如,如果您在 64 位操作系统上以 32 位兼容模式运行 32 位应用程序,那么它很可能会使用 32 位字长(当然,它是这样编译的)。哦,是的,它也可能取决于编译器。

"And your compiler flags..."(Thanks, Kay!)

“还有你的编译器标志......”(谢谢,凯!)

回答by SecurityMatt

If you care about the exact size of the variables use

如果您关心变量的确切大小,请使用

#include <stdint.h>

And then use the fixed-width types defined there:

然后使用那里定义的固定宽度类型:

uint8_t
uint16_t
uint32_t
uint64_t

or their signed cousins

或者他们签名的堂兄弟

int8_t
int16_t
int32_t
int64_t

Do not rely on the sizes of native types in C. Different compilers have different rules.

不要依赖 C 中本机类型的大小。不同的编译器有不同的规则。

回答by NovaDenizen

The sizes are set in stone by the compiler at compile-time, because the compiler has to emit size-specific instructions, lay out the members in structs, and know struct sizes for all required address calculations.

大小由编译器在编译时确定,因为编译器必须发出特定于大小的指令,在结构中布置成员,并知道所有所需地址计算的结构大小。

So if you compile your source into a 64-bit binary and run it on a bunch of different systems, the types will have the same sizes on each run, if the system supports the binary at all.

因此,如果您将源代码编译为 64 位二进制文​​件并在一堆不同的系统上运行它,那么如果系统完全支持二进制文件,则每次运行时这些类型的大小都相同。

If you then compile the source into a 32-bit binary or use different compiler switches, when you run that on a bunch of different systems then the numbers may be different from the 64-bit case, but they will be consistent over all the different systems.

如果您随后将源代码编译为 32 位二进制文​​件或使用不同的编译器开关,那么当您在一堆不同的系统上运行它时,数字可能与 64 位情况不同,但它们将在所有不同的系统上保持一致系统。

回答by PQuinn

Yes, it depends on the hardware, OS, compiler and even language in some cases.

是的,在某些情况下,这取决于硬件、操作系统、编译器甚至语言。

But on x86 linux, longs will be 4 bytes on 32-bit platforms, rather than 8. The others I believe all remains the same (not sure about long double).

但是在 x86 linux 上,long 在 32 位平台上将是 4 个字节,而不是 8 个。我相信其他的都保持不变(不确定 long double)。

Anecdote:

轶事:

I've worked on a 24-bit system where the word size was 24 bits, and every native type was 1 word in size. sizeof(char)? 1 (ie: 24 bits). sizeof(int)? 1 (ie: 24 bits). Etc. Fun!

我曾在 24 位系统上工作过,其中字长为 24 位,每个原生类型的大小为 1 个字。大小(字符)?1(即:24 位)。大小(整数)?1(即:24 位)。等等 好玩!