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

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

下面是我关于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?

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

float - data type

float

double - data type

double is used to define BIG floating point numbers. It reserves twice

char - data type

char

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

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:

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.

相关文档
最新文档