C 数据类型

C语言有五种基本数据类型:字符、整型、单精度实型、双精度实型和空类型。尽管这几种类型数据的长度和范围随处理器的类型和C语言编译程序的实现而异,但以b i t为例,整数与CPU字长相等,一个字符通常为一个字节,浮点值的确切格式则根据实现而定。对于多数微机,表2 - 1给出了五种数据的长度和范围。
C语言的数据类型
表中的长度和范围的取值是假定C P U的字长为1 6 b i t。
C语言还提供了几种聚合类型(aggregate types),包括数组、指针、结构、共用体(联合)、位域和枚举。这些复杂类型在以后的章节中讨论。
除v o i d类型外,基本类型的前面可以有各种修饰符。修饰符用来改变基本类型的意义,以便更准确地适应各种情况的需求。修饰符如下:
• signed(有符号)。
• unsigned(无符号)。
• long(长型符)。
• short(短型符)。修饰符s i g n e d、s h o r t、l o n g和u n s i g n e d适用于字符和整数两种基本类型,而l o n g还可用于d o u b l e(注意,由于long float与d o u b l e意思相同,所以A N S I标准删除了多余的long float)。
表2 - 2给出所有根据A N S I标准而组合的类型、字宽和范围。切记,在计算机字长大于1 6位的系统中,short int与signed char可能不等。
C语言的数据类型
*表中的长度和范围的取值是假定C P U的字长为1 6 b i t。因为整数的缺省定义是有符号数,所以s i n g e d这一用法是多余的,但仍允许使用。某些实现允许将u n s i g n e d用于浮点型,如unsigned double。但这一用法降低了程序的可移

植性,故建议一般不要采用。为了使用方便,C编译程序允许使用整型的简写形式:

• short int 简写为s h o r t。
• long int 简写为l o n g。
• unsigned short int 简写为unsigned short。
• unsigned int 简写为u n s i g n e d。
• unsigned long int 简写为unsigned long。
即,i n t可缺省。


void display(char *str , int size)
{
 cout<<str<<" :\t"<<size <<endl ;
}
void main()
{
 display( "sizeof(enum)" ,sizeof(enum) ) ;
 display( "sizeof(void *)" ,sizeof(void *) ) ;

 cout<<"\n" ;
 display( "sizeof(bool)" ,sizeof(bool) ) ;
 display( "sizeof(char )" ,sizeof(char ) ) ;
 display( "sizeof(unsigned char )" ,sizeof(unsigned char ) ) ;

 cout<<"\n" ;
 display( "sizeof( int)" ,sizeof( int) ) ;
 display( "sizeof(unsigned (int) )" ,sizeof(unsigned int ) ) ;
 display( "sizeof( short (int)  )" ,sizeof(short int ) ) ;
 display( "sizeof(short unsigned int = unsigned short (int) )" ,sizeof(short unsigned int ) ) ;
 display( "sizeof( long (int) )" ,sizeof( long int ) ) ;
 display( "sizeof(long unsigned int = unsigned long (int) )" ,sizeof(long unsigned int ) ) ;

 cout<<"\n" ;
 display( "sizeof( float )" ,sizeof(  float ) ) ;
 display( "sizeof( long float = double )" ,sizeof( long float ) ) ;
 
 cout<<"\n" ;
 display( "sizeof( dounble )" ,sizeof( double ) ) ;
 display( "sizeof(long dounble )" ,sizeof( long double ) ) ;
 
 cout<<"\n\n" ;
}

32bit---compute :

输出:

sizeof(enum) :  4
sizeof(void *) :        4

sizeof(bool) :  1
sizeof(char ) : 1
sizeof(unsigned char ) :        1

sizeof( int) :  4
sizeof(unsigned (int) ) :       4
sizeof( short (int)  ) :        2
sizeof(short unsigned int = unsigned short (int) ) :    2
sizeof( long (int) ) :  4
sizeof(long unsigned int = unsigned long (int) ) :      4

sizeof( float ) :       4
sizeof( long float = double ) : 8

sizeof( dounble ) :     8
sizeof(long dounble ) : 8

原文地址:https://www.cnblogs.com/sunkang/p/2038837.html