用C基本数据类型的大小 [英] size of basic data types in C

查看:111
本文介绍了用C基本数据类型的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些网站上复制的示例程序。

  INT主要(无效)
{
   INT的答案;
   短X = 1;
   长Y = 2;
   浮U = 3.0;
   双V = 4.4;
   长双W = 5.54;
   焦C ='P';   的typedef枚举
   {
      kAttributeInvalid,
      kBooleanAttributeActive,
      kBooleanAttributeAlarmSignal,
      kBooleanAttributeAlign64,
      kBooleanAttributeAutoNegotiationComplete,
   } codes_t;  / * __DATE__,__TIME__,__FILE__,__LINE__是predefined符号* /
  #如果0
  的printf(日期:%S \\ n,__DATE__);
  的printf(时间:%S \\ n,__TIME__);
  的printf(文件:%s \\ n,__FILE__);
  的printf(行数:%d \\ n,__LINE__);
  #万一  / *各种类型的大小* /
  的printf(INT%祖\\ n的大小的sizeof(回答));
  的printf(短%祖\\ n的大小的sizeof(X));
  的printf(%长祖\\ n的大小的sizeof(Y));
  的printf(浮动%祖\\ n的大小的sizeof(U));
  的printf(双祖%\\ n的大小的sizeof(V));
  的printf(长双祖%\\ n的大小的sizeof(W));
  的printf(%炭祖\\ n的大小的sizeof(C));
  的printf(%枚举祖\\ n的大小的sizeof(codes_t));  返回0;
}

我跑这个程序,输出,我得到如下:

 的INT 4的大小
短2的大小
长8大小
浮子4的尺寸
双8的大小
长双16的尺寸
炭1的尺寸
枚举4的尺寸

我运行这是运行64位Ubuntu.My问题Linux的PC上,如果我是运行在32位机器上相同的程序,我会看到换言之不同results.Or做的大小基本数据类型取决于


  1. 处理器

  2. 操作系统

  3. 别的什么


解决方案

在符合其安装一些库[可能只是glibc的],在它的32位变量,你应该能够通过使用<$ C $自己试用C>的gcc -m32 myprog.c中 [或铛-m32 myprog.c中]。

不过,这已上市,这将如果从64位x86 Linux系统迁移到32位x86的Linux系统,使用基于GCC编译器改变你的项目,唯一的事情就是<$ C的大小$ C>长。注86,GCC等重资格 - 编译器有很大的自由。有人可以编写一个Linux编译器使用16位 INT 和64位 32位系统上没有巨大困难的量。使用该编译器来编译Linux内核和许多的Linux工具可能会失败[最有可能包括编译 GCC 与编译器。但你真的不能说,在此架构或在这个操作系统或这个编译器......也没有资格,其他参数是什么。

案例分析:微软的C / C ++编译器有一个为32位甚至64位系统。为什么,我听到你问?由于大量的Windows API函数使用作为一个32位值从当Windows是英特尔三百八十六分之二百八十六处理器的16位操作系统的遗产。因为(一些)系统调用是Windows向后兼容很远的路,那就是16位系统编写将在64位仍然有效的Windows code [除非code使用了一些非常不寻常系统调用,当然,在风格将看起来有点古老。更改来一个64位的值会打破一些functioanilty,所以在MS编译器的家伙决定坚持 = 32位。如果你想64位整数,你必须使用长长的int64_t 或别的东西,而不是。当然,这打破了一些code,它假定的sizeof(长)==的sizeof(无效*)。我们希望,大多数这类code已经固定...

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

  1. processor
  2. Operating System
  3. anything else

解决方案

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].

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 int and 64-bit long on 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 gcc with 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.

Case in point: A Microsoft C/C++ compiler has a long that is 32 bit even on 64-bit systems. Why, I hear you ask? Because a large number of Windows API functions use long as 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 long to 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 long or int64_t or 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...

这篇关于用C基本数据类型的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆