C语言中的整形,字符型,浮点型

1、有符号整形

        在C语言中,有符号整形变量a用signed int a表示,通常我们写的int a默认为有符号整形。

         根据程序编译器的不同,整形定义的字节数不同。常用的单片机编译器,如KEIL下,51类单片机的C语言中,int代表2个byte(16位);如果是32位ARM处理器的C语言中,则int代表4个byte(32位)。而不少PC端软件的编译器则会根据操作系统或处理器(如64位XP)把int定义为8 byte(64位),(如32位XP)把int定义为4 byte(32位)。

在32位的xp系统中,对于有符号整形变量a, signed short  int a,a为16位。

        注意一下取值范围。若在32位系统中,signed int a, 则a范围[-2^31 , 2^31 -1] 即 [-2147483648,2147483647]。

        在32位系统中,signed long long int a,a占8个字节。

                                 signed long int a,   a占4个字节。

                                 signed short int a,  a占2个字节。


       %d是有符号整数格式,它的最高位是符号位,十进制数
       %u是无符号整数格式,它的最高位是有效数字,十进制数
       %o对应八进制数
       %x对应十六进制数
       %f对应float单精度浮点型数据
       %lf对应double双精度浮点型数据

          对于整形,INT_MAX表示有符号整形最大值,INT_MIN表示有符号整形最小值。

                           UINT_MAX表示无符号整形最大值。对应头文件<limits.h>

         对整形用printf输出,若为有符号整型,则用“%d”,对无符号用“%u”输出,若都用“%d”则容易出错,对应无符号数有可能溢出。

如,signed int s;

        s=INT_MAX;

        printf("%d\n",s); 

        unsigned int t;

        t=UINT_MAX;

         printf("%d\n",t);  /*错误,会输出-1,应该用%u*/

2、无符号整形

        在C语言中,无符号整形变量b用unsigned int b表示

        在32位的xp系统中,对于无符号整形变量b, signed short  int b,b为16位。

        注意一下取值范围。若在32位系统中,signed int b, 则b范围[0 , 2^32 -1] 即 [0,4294967295]。

3、字符型

        在 C语言中,字符c用char c表示,char在标准中是unsigned, 编译器可以实现为带符号的,也可以实现为不带符号的。
      在VC6.0及linux下char 范围为[-128,127],如下
       char a=127;
        a=a+1;          /*现在a的值为-128*/
      
       unsigned char b=255;
        b+=1;         /*现在b的值为0*/
         

4、浮点型

       浮点型包括单浮点型float,双浮点型double,浮点型数据均为有符号型。

有关运算

在进行运算时,注意有符号与无符号变量的影响。如 
unsigned int a=1;
int b=-1;
int c=-1;

if(a>b)
    c=1;
else
    c=0;
结果c=0。
-----------------------

面试题:

unsigned int a = 6;

int b = -20;

int c;

(a+b>6)?(c=1):(c=0)

问:

c=?

 

看下面这段:

K & R A.6.5
Arithmetic Conversions
First, if either operand is long double, the other is converted to long double.
Otherwise, if either operand is double, the other is converted to double.
Otherwise, if either operand is float, the other is converted to float.
Otherwise, the integral promotions are performed on both operands; then, if either operand is unsigned long int, the other is converted to unsigned long int.
Otherwise, if one operand is long int and the other is unsigned int, the effect depends on whether a long int can represent all values of an unsigned int; if so, the unsigned int operand is converted to long int; if not, both are converted to unsigned long int.
Otherwise, if one operand is long int, the other is converted to long int.
Otherwise, if either operand is unsigned int, the other is converted to unsigned int.
Otherwise, both operands have type int.

简单翻译如下:
如果任一个操作数是long double, 则另一个要转换为long double
如果任一个操作数是double, 则另一个要转换为double
如果任一个操作数是float, 则另一个要转换为float
此外整数运算符升级对两个操作数都有影响;
如果任一个操作数是unsigned long int, 则另一个要转换为unsigned long int
如果一个操作数是long int, 另一个是unsigned int, 如果long int可以表示结果,则unsigned int要转换为long int;
否则两个都转换为unsigned long int
如果任一个操作数是long int, 则另一个要转换为long int
如果任一个操作数是unsigned int, 则另一个要转换为unsigned int
除此之外,两个操作数都应是int

 

倒数第二句是解本题的关键:

如果任一个操作数是unsigned int, 则另一个要转换为unsigned int

首先将b=-20用补码表示:

20的二进制:

0000 0000 0001 0100

取反:

1111 1111 1110 1011

再加1:

1111 1111 1110 1100

把第一个代表负号的1去掉,

0111 1111 1110 1100

把上面作为unsigned int 是十进制32748

财(a+b)肯定大于6,

则c=1


 --------------------------------

以下内容来自:http://www.cnblogs.com/wensheng/archive/2009/10/14/1583589.html

int a=100;
unsigned 
int i=99;
cout 
< <i-< <endl;
其结果输出是4294967295;
若改为unsigned 
short i=99;
输出就是
-1

楼主问为什么?我确实第一反应回答不上来。也只能根据结果做出猜测,
结果一楼的朋友说了这么个原则:“二者长度相同,按照被减数的的类型; 二者长度不同,按长的”,自己一想也对!
但是结果又看了下面朋友的回答,其中有个比较权威的人事说了:
unsigned int比int"大"
int 比 unsigned short"大"

所以第一个向老大看齐,就是unsigned int
第二个的老大是int
就感到无比的迷惑了,接着一楼的朋友发现了自己的错误,紧接着跟帖声明了自己的错误(值得学习)


同时在做此题的时候还学到另一个知识:
就是用printf的问题
我对上面的输出代码改为

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值