C语言数据类型及表示范围.docx

上传人:b****6 文档编号:7837042 上传时间:2023-01-26 格式:DOCX 页数:56 大小:24.29KB
下载 相关 举报
C语言数据类型及表示范围.docx_第1页
第1页 / 共56页
C语言数据类型及表示范围.docx_第2页
第2页 / 共56页
C语言数据类型及表示范围.docx_第3页
第3页 / 共56页
C语言数据类型及表示范围.docx_第4页
第4页 / 共56页
C语言数据类型及表示范围.docx_第5页
第5页 / 共56页
点击查看更多>>
下载资源
资源描述

C语言数据类型及表示范围.docx

《C语言数据类型及表示范围.docx》由会员分享,可在线阅读,更多相关《C语言数据类型及表示范围.docx(56页珍藏版)》请在冰豆网上搜索。

C语言数据类型及表示范围.docx

C语言数据类型及表示范围

C语言各种数据类型在系统中占的字节和取值范围

基本类型包括字节型(char)、整型(int)和浮点型(float/double)。

定义基本类型变量时,可以使用符号属性signed、unsigned(对于char、int),和长度属性short、long(对于int、double)对变量的取值区间和精度进行说明。

下面列举了Dev-C++下基本类型所占位数和取值范围:

符号属性长度属性基本型所占位数取值范围输入符举例输出符举例

----char8-2^7~2^7-1%c%c、%d、%u

signed--char8-2^7~2^7-1%c%c、%d、%u

unsigned--char80~2^8-1%c%c、%d、%u

[signed]short[int]16-2^15~2^15-1%hd

unsignedshort[int]160~2^16-1%hu、%ho、%hx

[signed]--int32-2^31~2^31-1%d

unsigned--[int]320~2^32-1%u、%o、%x

[signed]long[int]32-2^31~2^31-1%ld

unsignedlong[int]320~2^32-1%lu、%lo、%lx

[signed]longlong[int]64-2^63~2^63-1%I64d

unsignedlonglong[int]640~2^64-1%I64u、%I64o、%I64x

----float32+/-3.40282e+038%f、%e、%g

----double64+/-1.79769e+308%lf、%le、%lg%f、%e、%g

--longdouble96+/-1.79769e+308%Lf、%Le、%Lg

几点说明:

1.注意!

表中的每一行,代表一种基本类型。

“[]”代表可省略。

例如:

char、signedchar、unsignedchar是三种互不相同的类型;

int、short、long也是三种互不相同的类型。

可以使用C++的函数重载特性进行验证,如:

voidFunc(charch){}

voidFunc(signedcharch){}

voidFunc(unsignedcharch){}

是三个不同的函数。

2.char/signedchar/unsignedchar型数据长度为1字节;

char为有符号型,但与signedchar是不同的类型。

注意!

并不是所有编译器都这样处理,char型数据长度不一定为1字节,char也不一定为有符号型。

3.将char/signedchar转换为int时,会对最高符号位1进行扩展,从而造成运算问题。

所以,如果要处理的数据中存在字节值大于127的情况,使用unsignedchar较为妥当。

程序中若涉及位运算,也应该使用unsigned型变量。

4.char/signedchar/unsignedchar输出时,使用格式符%c(按字符方式);或使用%d、%u、%x/%X、%o,按整数方式输出;输入时,应使用%c,若使用整数方式,Dev-C++会给出警告,不建议这样使用。

5.int的长度,是16位还是32位,与编译器字长有关。

16位编译器(如TC使用的编译器)下,int为16位;32位编译器(如VC使用的编译器cl.exe)下,int为32

位。

6.整型数据可以使用%d(有符号10进制)、%o(无符号8进制)或%x/%X(无符号16进制)方式输入输出。

而格式符%u,表示unsigned,即无符号10进制方式。

7.整型前缀h表示short,l表示long。

输入输出short/unsignedshort时,不建议直接使用int的格式符%d/%u等,要加前缀h。

这个习惯性错误,来源于TC。

TC下,int的长度和默认符号属性,都与short一致,于是就把这两种类型当成是相同的,都用int方式进行输入输出。

8.关于longlong类型的输入输出:

"%lld"和"%llu"是linux下gcc/g++用于longlongint类型(64bits)输入输出的格式符。

而"%I64d"和"%I64u"则是MicrosoftVC++库里用于输入输出__int64类型的格式说明。

Dev-C++使用的编译器是Mingw32,Mingw32是x86-win32gcc子项目之一,编译器核心还是linux下的gcc。

进行函数参数类型检查的是在编译阶段,gcc编译器对格式字符串进行检查,显然它不认得"%I64d",所以将给出警“unknownconversiontypecharacter`I'informat”。

对于"%lld"和"%llu",gcc理所当然地接受了。

Mingw32在编译期间使用gcc的规则检查语法,在连接和运行时使用的却是Microsoft库。

这个库里的printf和scanf函数当然不认识linuxgcc下"%lld"和"%llu",但对"%I64d"和"%I64u",它则是乐意接受,并能正常工作的。

9.浮点型数据输入时可使用%f、%e/%E或%g/%G,scanf会根据输入数据形式,自动处理。

输出时可使用%f(普通方式)、%e/%E(指数方式)或%g/%G(自动选择)。

10.浮点参数压栈的规则:

float(4字节)类型扩展成double(8字节)入栈。

所以在输入时,需要区分float(%f)与double(%lf),而在输出时,用%f即可。

printf函数将按照double型的规则对压入堆栈的float(已扩展成double)和double型数据进行输出。

如果在输出时指定%lf格式符,gcc/mingw32编译器将给出一个警告。

11.Dev-C++(gcc/mingw32)可以选择float的长度,是否与double一致。

12.前缀L表示long(double)。

虽然longdouble比double长4个字节,但是表示的数值范围却是一样的。

longdouble类型的长度、精度及表示范围与所使用的编译器、操作系统等有关。

转:

1.整型数据类型

C定义了5种整型数据类型。

整型数据类型表

序号

类型名称

说明

字节数

取值范围

1

signedchar

有符号的单字节整数类型

1

-128~+127

2

shortint

短整型

2

-32768~+32767

3

int

整型

4

-2147438648~+2147438647

4

longint

长整型

4

-2147438648~+2147438647

5

longlongint

长长整型

8

-9223372036854775808~+-9223372036854775807

例输出各种整型类型的字节数

#include

intmain(void){

printf("sizeof(signedchar)=%d/n",sizeof(signedchar));

printf("sizeof(shortint)=%d/n",sizeof(shortint));/*sizeof的结果都是int型*/

printf("sizeof(int)=%d/n",sizeof(int));

printf("sizeof(longint)=%d/n",sizeof(longint));

printf("sizeof(longlongint)=%d/n",sizeof(longlongint));

return0;

}

编译和运行结果

[root@localhostccc]#gccc15.c

[root@localhostccc]#./a.out

sizeof(signedchar)=1

sizeof(shortint)=2

sizeof(int)=4

sizeof(longint)=4

sizeof(longlongint)=8

程序说明:

sizeof是字节操作符,使用方式,sizeof(数据类型)。

sizeof的作用是得到数据类型所占的字节数。

我们运行程序使用的环境是Redhat5Linux,编译器是GCC。

2.无符号整数类型

对应有符号类型,还有无符号整数类型。

无符号整数类型表

序号

类型名称

字节数

取值范围

1

unsignedchar

1

0~255

2

unsignshortint

2

0~65535

3

unsignedint

4

0~4294967295

4

unsignedlongint

4

0~4294967295

5

unsignlonglongint

8

0~184********709551615

例输出各种无符号整数类型的字节数

#include

intmain(void){

printf("sizeof(unsignedchar)=%d/n",sizeof(unsignedchar));

printf("sizeof(unsignedshortint)=%d/n",sizeof(unsignedshortint));/*sizeof的结果都是int型*/

printf("sizeof(unsignedint)=%d/n",sizeof(unsignedint));

printf("sizeof(unsignedlongint)=%d/n",sizeof(unsignedlongint));

printf("sizeof(unsignedlonglongint)=%d/n",sizeof(unsignedlonglongint));

return0;

}

编译和运行结果

[root@localhostccc]#gccc16.c

[root@localhostccc]#./a.out

sizeof(unsignedchar)=1

sizeof(unsignedshortint)=2

sizeof(unsignedint)=4

sizeof(unsignedlongint)=4

sizeof(unsignedlonglongint)=8

3.整型常量

整型常量是指用以表示整型数值的常量,分为短整型(shortint)、整型(int)、长整型(longint)和长长整型(longlongint)四种。

C默认整型(int)。

各种类型整型常量进制表示表(后缀不区分大小写)

序号

数据类型

八进制

十进制

十六进制

1

整型

0112

74

0x4a

2

长整型(l)

0112l

74l

0x4al

3

长长整型(ll)

0112ll

74ll

0x4all

4

无符号整型(u)

0112u

74u

0x4au

5

无符号长整型(ul)

0112ul

74ul

0x4aul

6

无符号长长整型(ull)

0112ull

74ull

0x4aull

4.字符数据类型

C语言中字符型数据只有一种,即char型数据。

一般也把char直接称为字符型。

字符型占用内存空间最少,一般占用一个字节,存储在char类型变量的整数可以表示为有符号或无符号的值,这取决于编译器。

例字符型数据类型的字节长度

#include

intmain(void){

printf("sizeof(char)=%d/n",sizeof(char));

printf("sizeof(signedchar)=%d/n",sizeof(signedchar));

printf("sizeof(unsignedchar)=%d/n",sizeof(unsignedchar));

return0;

}

编译和运行结果

[root@localhostccc]#gccc17.c

[root@localhostccc]#./a.out

sizeof(char)=1

sizeof(signedchar)=1

sizeof(unsignedchar)=1

5.字符变量

字符变量是用于存储字符型数值的变量。

字符型变量也分为两种:

有符号和无符号型。

语法结构:

[signed]charch1;

[unsigned]charch2;

#include

intmain(void){

charch1='/n';

unsignedcharch2='/t';

charch3=48;

printf("ch1=[%c],[%d]/n",ch1,ch1);

printf("ch2=[%c],[%d]/n",ch2,ch2);

printf("ch3=[%c],[%d]/n",ch3,ch3);

return0;

}

编译和运行结果

[root@localhostccc]#gccc18.c

[root@localhostccc]#./a.out

ch1=[

],[10]

ch2=[],[9]

ch3=[0],[48]

程序说明:

转义符'/n'是换行符,第一个printf函数中%c处会替换为'/n',输出到终端上则会替换为换行,我们可以看到输出的第一个方括号中间发生换行。

%d是c1的ASCII码值代替。

转义符'/t'是水平制表符,第二个printf里的%c会替代为'/t',输出到终端上。

数值48对应的ASCII码是0,所以对应输出到终端上是一个0。

6.浮点型数据类型

C语言定义了三种浮点数据类型:

?

float,单精度

?

double,双精度

?

longdouble,长双精度

C标准中对不同类型的浮点数有不同的规定,编译器不同或硬件条件不同,字节长度也不相同。

例测试三种浮点型字节长度

#include

intmain(void){

printf("sizeof(float)=%d/n",sizeof(float));

printf("sizeof(double)=%d/n",sizeof(double));

printf("sizeof(longdouble)=%d/n",sizeof(longdouble));

return0;

}

编译和运行结果

[root@localhostccc]#gccc19.c

[root@localhostccc]#./a.out

sizeof(float)=4

sizeof(double)=8

sizeof(longdouble)=12

浮点型的字节长度、精度、数量级范围和输出输入格式表

序号

数据类型

字节长度

精度

数量级范围

printf和scanf格式

1

float(f)

4

7

-38~38

%f

2

double

8

约16

-308~308

%f

3

longdouble

(1)

12

约19

-4932~4932

%llf

7.浮点型精度

浮点型精度从低到高排列为float、double和longlongdouble。

例检查浮点型精度

#include

intmain(void){

floatf=0.9876543210123456789012f;/*可以不使用f后缀,但可能会出现warning*/

doubled=0.9876543210123456789012;

longdoubleld=0.9876543210123456789012l;/*必须使用后缀l或L*/

printf("f/t=%.25f/n",f);

printf("d/t=%.25lf/n",d);

printf("ld/t=%.25llf/n",ld);

return0;

}

编译和运行结果

[root@localhostccc]#gccc20.c

[root@localhostccc]#./a.out

f=0.9876543283462524414062500

d=0.9876543210123456262294894

ld=0.9876543210123456789217150

8.浮点型的存储方式

浮点型数值以科学计数法的表示形式存储在内存中。

浮点型的内存形式包含三个部分:

1)符号位

符号位浮点型的符号位只有一位,为最高位。

该位为1,表示负数,该位为0,为非负数。

2)指数位

浮点型的指数位以补码形式存储,是科学计数法的指数部分。

3)基数位

基数位是浮点型的最后一位,这个位决定数值的精度。

浮点型储存分段表

序号

数据类型

符号位

指数位

基数位

偏差值

1

float

1

8

23

127

2

double

1

11

52

1023

3

longdouble

1

15

64

16383

例检测float、double和longdouble的存储状态

#include

intmain(void){

floatfone=2.0;

floatftwo=2.5;

doubledone=2.0;

doubledtwo=2.5;

longdoubleldone=2.0;

longdoubleldtwo=2.5;

/*输出float型数据在内存中的存储内容*/

printf("float(2.0)=%08x/n",*(int*)(&fone));

printf("float(2.5)=%08x/n",*(int*)(&ftwo));

/*输出double型数据在内存中的存储内容*/

printf("double(2.0)=%016llx/n",*(longlong*)(&done));

printf("double(2.5)=%016llx/n",*(longlong*)(&dtwo));

/*输出longdouble型数据在内存中的存储内容*/

printf("londou(2.0)=%08x%08x%08x/n",

*(((int*)(&ldone))+2),

*(((int*)(&ldone))+1),

*(((int*)(&ldone))));

printf("londou(2.5)=%08x%08x%08x/n",

*(((int*)(&ldtwo))+2),

*(((int*)(&ldtwo))+1),

*(((int*)(&ldtwo))));

return0;

}

编译和运行结果

[root@localhostccc]#gccc21.c

[root@localhostccc]#./a.out

float(2.0)=40000000

float(2.5)=40200000

double(2.0)=4000000000000000

double(2.5)=4004000000000000

londou(2.0)=000040008000000000000000

londou(2.5)=00004000a000000000000000

来自:

albert_wei>《未命名》

献花(0)

+1

分享:

人人网

开心网

搜狐微博

推荐给朋友

举报

1.整型数据类型

C定义了5种整型数据类型。

整型数据类型表

序号

类型名称

说明

字节数

取值范围

1

signedchar

有符号的单字节整数类型

1

-128~+127

2

shortint

短整型

2

-32768~+32767

3

int

整型

4

-2147438648~+2147438647

4

longint

长整型

4

-2147438648~+2147438647

5

longlongint

长长整型

8

-9223372036854775808~+-9223372036854775807

例输出各种整型类型的字节数

#include

intmain(void){

printf("sizeof(signedchar)=%d/n",sizeof(signedchar));

printf("sizeof(shortint)=%d/n",sizeof(shortint));/*sizeof的结果都是int型*/

printf("sizeof(int)=%d/n",sizeof(int));

printf("sizeof(longint)=%d/n",sizeof(longint));

printf("sizeof(longlongint)=%d/n",sizeof(longlongint));

return0;

}

编译和运行结果

[root@localhostccc]#gccc15.c

[root@localhostccc]#./a.out

sizeof(signedchar)=1

sizeof(shortint)=2

sizeof(int)=4

sizeof(longint)=4

sizeof(longlongint)=8

程序说明:

sizeof是字节操作符,使用方式,sizeof(数据类型)。

sizeof的作用是得到数据类型所占的字节数。

我们运行程序使用的环境是Redhat5Linux,编译器是GCC。

2.无符号整数类型

对应有符号类型,还有无符号整数类型。

无符号整数类型表

序号

类型名称

字节数

取值范围

1

unsignedchar

1

0~255

2

unsignshortint

2

0~65535

3

unsignedint

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 电子电路

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1