C中基本数据类型的大小

liv*_*hak 4 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;
}
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

  1. 处理器
  2. 操作系统
  3. 还要别的吗

小智 7

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

也许.或者可能不是.

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

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

"而你的编译器标志......"(谢谢,凯!)

  • 和你的编译器标志. (2认同)