C语言中各种数据类型长度的总结



下面是我关于 C 语言中各种数据类型长度的总结: ( 参考 The C Programming Language)

 

1. c 语言中的整数类型有 char, short, int, long 等几种 , 下面是 C 语言对每种数据类型长度的规定 :

(a). short long 类型的长度不相同

(b). int 类型通常同具体机器的物理字长相同

(c). short 通常是 16bits, int 通常是 16bits or 32bits 每种编译器可以根据硬件的不同自由确定 , 但是 short int 必须最少是 16bits, long 类型必须最少是 32bits, 并且 short 必须比 int long 类型要短。

2. sizeof() 运算符返回的是一种数据类型中所包含的字节数 (bytes), AnsiC 规定 sizeof(char) 必须返回 1 ,当 sizeof 作用于数组时 , 返回的是数组中所有成员所占的字节数 ( 注意并不是数组中成员的个数 ), sizeof() 作用于结构体和公用体时 , 返回的不仅仅是数据成员总的字节数 , 还包括编译器为了实现字节对其而填充的那些字节。

 

以前写程序也隐隐约约的懂得这些规则,但是一直以为 char 类型必须是 8bits 的,但是最近做了一个嵌入式 DSP 项目,编译器手册上明明写着 char 类型就是 16bits 的,无奈翻出 "The C Programming Language" 一查才发现 ANSI C 对于 char 类型的长度并没有作硬性规定。以前写程序不太注意数据类型的可移植性 , 这次项目中用到的以前的代码都要重新检查数据类型长度的问题。

 

C Data types.


Variable definition

C has a concept of 'data types ' which are used to define a variable before its use.

The definition of a variable will assign storage for the variable and define the type of data that will be held in the location.

So what data types are available?

int

float

double

char

void

enum

Please note that there is not a boolean data type. C does not have the traditional view about logical comparison, but thats another story.

Recent C++ compilers do have a boolean datatype.


int - data type

int is used to define integer numbers.

 

    {

        int Count;

        Count = 5;

    }


 

float - data type

float is used to define floating point numbers.

 

    {

        float Miles;

        Miles = 5.6;

    }


 

double - data type

double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.

 

    {

        double Atoms;

        Atoms = 2500000;

    }


 

char - data type

char defines characters.

 

    {

        char Letter;

        Letter = 'x';

    }

 

Modifiers

The three data types above have the following modifiers.

  • short
  • long
  • signed
  • unsigned

The modifiers define the amount of storage allocated to the variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:

 

        short int <=    int <= long int

            float <= double <= long double

What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'. What this means in the real world is:

 

                 Type  Bytes  Bits                Range

 

            short int    2      16          -32,768 -> +32,767          (32kb)

   unsigned short int    2      16                0 -> +65,535          (64Kb)

         unsigned int    4      32                0 -> +4,294,967,295   ( 4Gb)

                  int    4      32   -2,147,483,648 -> +2,147,483,647   ( 2Gb)

             long int    4      32   -2,147,483,648 -> +2,147,483,647   ( 2Gb)

          signed char    1       8             -128 -> +127

        unsigned char    1       8                0 -> +255

                float    4      32

               double    8      64

          long double   12      96

These figures only apply to todays generation of PCs. Mainframes and midrange machines could use different figures, but would still comply with the rule above.

You can find out how much storage is allocated to a data type by using the sizeof operator.

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值