C语言中基本整型数据类型

C语言中的基本整型数据类型

整型家族包括字符、短整型、整型、长整型,它们都分为有符号和无符号两种版本

整型值相互之间的大小的规则:

长整型至少应该和整型一样长,而整型至少应该和短整型一样长

一般情况下各种类型在32、64位机上的大小

C语言中基本整型数据类型

long,unsigned long pointer为机器全字长,其实也就是这几种类型的大小和CPU的位数是一样的。

下面的程序是在X86架构环境上,使用gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC)编译得到的

#include <stdio.h>

int main()
{
        printf("the char range is           %-20hhd~%20hhu, size is %u\n", 0x80, 0x7f, sizeof(char));
        printf("the usigned char range is   %-20hhu~%20hhu, size is %u\n", 0x0, 0xff, sizeof(unsigned char));
        printf("the short range is          %-20hd~%20hu, size is %u\n", 0x8000, 0x7fff, sizeof(short));
        printf("the unsigned short range is %-20hu~%20hu, size is %u\n", 0x0, 0xffff, sizeof(unsigned short));
        printf("the int range is            %-20d~%20u, size is %u\n", 0x80000000, 0x7fffffff, sizeof(int));
        printf("the unsigned int range is   %-20u~%20u, size is %u\n", 0x0, 0xffffffff,  sizeof(unsigned int));
        printf("the long range is           %-20ld~%20lu, size is %u\n", 0x80000000, 0x7fffffff, sizeof(long));
        printf("the unsigned long range is  %-20lu~%20lu, size is %u\n", 0x0, 0xffffffff, sizeof(unsigned long));
        printf("the llong range is          %-20lld~%20llu, size is %u\n", 0x8000000000000000, 0x7fffffffffffffff, sizeof(long long));
        printf("the unsigned llong range is %-20lld~%20llu, size is %u\n", 0x0ll, 0xffffffffffffffff, sizeof(unsigned long long));
        printf("default constant size is %u %u %u\n", sizeof(‘b‘), sizeof(0x0), sizeof(0x0ll));
}

运行结果

the char range is           -128                ~                 127, size is 1
the usigned char range is   0                   ~                 255, size is 1
the short range is          -32768              ~               32767, size is 2
the unsigned short range is 0                   ~               65535, size is 2
the int range is            -2147483648         ~          2147483647, size is 4
the unsigned int range is   0                   ~          4294967295, size is 4
the long range is           -2147483648         ~          2147483647, size is 4
the unsigned long range is  0                   ~          4294967295, size is 4
the llong range is          -9223372036854775808~ 9223372036854775807, size is 8
the unsigned llong range is 0                   ~18446744073709551615, size is 8

default constant size is 4 4 8


整型字面值(整型常量)

字面值这个术语是字面值常量的缩写------这是一种实体,指定了自身的值,并且不允许发生改变。

当程序内出现整型常量时,它是属于整型家族类型中的哪一种?答案取决于常量是如何书写的,可以在有些常量的后面添加一个后缀来改变缺省规则。在整数常量后面添加L或l,可以使这个整数被解释为long整型值,字符U或u则用于吧数值指定为unsigned 整型值,如果一个常量后面添加这两个字符中的各一个,那么它就被解释为unsigned long整型值。对于字符常量。它们的类型总是int。不能再它们后面添加unsigned或者long后缀

C语言一般默认整型常量(字符常量/整型常量)类型为int,

如果使用格式字符%llu,printf会认为要输出的这个值的大小为8字节,所以如果对应的参数如果为0x0的话则有可能打印出乱七八糟的东西(因为它读了8个字节而不是4个字节)

另外printf整型变量时,打印的是有符号还是无符号取决于格式字符,与实际传入的参数类型及符号无关。

例如

int i = 0x80001010;

printf("%d\n", i);打印-2147479536 

printf("%u\n", i);打印2147487760

printf("%hu\n",i);打印4112高两位被截断,打印的实际是0x1010

printf("%hhu\n",i);打印16高三位被截断,打印的实际是0x10

所以请谨记使用printf打印变量的大小与符号与变量本身无关,即使强转也不行!应选择对应正确的格式字符


C语言中基本整型数据类型,布布扣,bubuko.com

C语言中基本整型数据类型

上一篇:VC++中CString类在不同环境中的区别


下一篇:JavaScript程序设计之常用窗口对象