c - C中基本数据类型的大小

标签 c types

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

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;
}

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

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

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

  1. 处理者
  2. 操作系统
  3. 还有什么

最佳答案

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 1) processor 2) Operating System 3) anything else

  1. 是,2. 是,3. 是,例如,如果您在 64 位操作系统上以 32 位兼容模式运行 32 位应用程序,那么它很可能会使用 32 位字长(当然,它是这样编译的)。哦,是的,它也可能取决于编译器。

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

关于c - C中基本数据类型的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14821738/

相关文章:

c - 在c中设计这个结构的最佳方法是什么?

无法使用在 Ubuntu 上链接的 SDL 库编译文件

c - 在 C 中使用转置的矩阵乘法

reactjs - TypeScript 条件类型 : Extract component props type from react component

清除屏幕和 kbhit() 函数

c++ - Windows CE 内部构件,TEB(线程环境 block )

c# - 适用于所有数据类型的动态 TryParse

c++ - 为什么 C++ 将表达式中的 signed int 转换为 unsigned int?

types - 为什么我的类型构造函数没有被识别

C 数据类型的范围