zoukankan      html  css  js  c++  java
  • C语言中的整形,字符型,浮点型

    1、有符号整形        文章出处http://blog.csdn.net/huguangshanse00/article/details/8469845                     什么时候我也能自己总结 = =。

            在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 ",s); 

            unsigned int t;

            t=UINT_MAX;

             printf("%d ",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
    ====================================================================

    C语言实现:字符串与整数、浮点数、无符号整数之间的转换常用函数  

    出处http://blog.sina.com.cn/s/blog_765acc35010135bb.html

    atof(将字符串转换成浮点型数)

    表头文件 #include <stdlib.h>
    定义函数 double atof(const char *nptr);
    函数说明
    atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('')才结 

       束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。
    返回值 返回转换后的浮点型数。
    附加说明 atof()与使用strtod(nptr,(char**)NULL)结果相同。
    范例
    #include<stdlib.h>
    main()
    {
    char *a=”-100.23”;
    char *b=”200e-2”;
    float c;
    c=atof(a)+atof(b);
    printf(“c=%.2f ”,c);
    }
    执行
    c=-98.23

    ////////////////////////////////////////////////////////////////////
    atoi(将字符串转换成整型数)

    表头文件 #include<stdlib.h>
    定义函数 int atoi(const char *nptr);
    函数说明 atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('')才结束转换,并将结果返回。
    返回值 返回转换后的整型数。
    附加说明 atoi()与使用strtol(nptr,(char**)NULL,10);结果相同。
    范例
    #include<stdlib.h>
    mian()
    {
    char a[]=”-100”;
    char b[]=”456”;
    int c;
    c=atoi(a)+atoi(b);
    printf(c=%d ”,c);
    }
    执行
    c=356
    //////////////////////////////////////////////////////////////////////////
    atol(将字符串转换成长整型数)

    表头文件 #include<stdlib.h>
    定义函数 long atol(const char *nptr);
    函数说明 atol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('')才结束转换, 并将结果返回。
    返回值 返回转换后的长整型数。
    附加说明 atol()与使用strtol(nptr,(char**)NULL,10);结果相同。
    范例
    #include<stdlib.h>
    main()
    {
    char a[]=”1000000000”;
    char b[]=” 234567890”;
    long c;
    c=atol(a)+atol(b);
    printf(“c=%d ”,c);
    }
    执行
    c=1234567890

    ////////////////////////////////////////////////////////////////

    gcvt(将浮点型数转换为字符串,取四舍五入)
    相关函数 ecvt,fcvt,sprintf
    表头文件 #include<stdlib.h>
    定义函数 char *gcvt(double number,size_t ndigits,char *buf);
    函 数说明 gcvt()用来将参数number转换成ASCII码字符串,参数ndigits表示显示的位数。gcvt()与ecvt()和fcvt()不同的地 方在于,gcvt()所转换后的字符串包含小数点或正负符号。若转换成功,转换后的字符串会放在参数buf指针所指的空间。
    返回值 返回一字符串指针,此地址即为buf指针。
    附加说明 范例 #include<stdlib.h>

    main()
    {
    double a=123.45;
    double b=-1234.56;
    char *ptr;
    int decpt,sign;
    gcvt(a,5,ptr);
    printf(“a value=%s ”,ptr);
    ptr=gcvt(b,6,ptr);
    printf(“b value=%s ”,ptr);
    }
    执行
    a value=123.45
    b value=-1234.56
    ///////////////////////////////////////////////////////////////////////
    strtod(将字符串转换成浮点数)
    表头文件 #include<stdlib.h>
    定义函数 double strtod(const char *nptr,char **endptr);
    函 数说明 strtod()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,到出现非数字或字符串结束时('')才结束转 换,并将结果返回。若endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr传回。参数nptr字符串可包含正负号、 小数点或E(e)来表示指数部分。如123.456或123e-2。
    返回值 返回转换后的浮点型数。
    附加说明 参考atof()。
    范例
    #include<stdlib.h>
    mian()
    {
    char a[]=”1000000000”;
    char b[]=”1000000000”;
    char c[]=”ffff”;
    printf(“a=%d ”,strtod(a,NULL,10));
    printf(“b=%d ”,strtod(b,NULL,2));
    printf(“c=%d ”,strtod(c,NULL,16));
    }
    执行
    a=1000000000
    b=512
    c=65535
    ////////////////////////////////////////////////////////////////////////////////////
    strtol(将字符串转换成长整型数)
    相关函数 atof,atoi,atol,strtod,strtoul
    表头文件 #include<stdlib.h>
    定义函数 long int strtol(const char *nptr,char **endptr,int base);
    函数说明
    strtol()会将参数nptr字符串根据参数base来转换成长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式,如 base值为10则采用10进制,若base值为16则采用16进制等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用 16进制做转换。一开始strtol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束 时('')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。
    返回值 返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。
    附加说明 ERANGE指定的转换字符串超出合法范围。
    范例
    #include<stdlib.h>
    main()
    {
    char a[]=”1000000000”;
    char b[]=”1000000000”;
    char c[]=”ffff”;
    printf(“a=%d ”,strtol(a,NULL,10));
    printf(“b=%d ”,strtol(b,NULL,2));
    printf(“c=%d ”,strtol(c,NULL,16));
    }
    执行
    a=1000000000
    b=512
    c=65535
    ///////////////////////////////////////////////////////////
    strtoul(将字符串转换成无符号长整型数)
    相关函数 atof,atoi,atol,strtod,strtol
    表头文件 #include<stdlib.h>
    定义函数 unsigned long int strtoul(const char *nptr,char **endptr,int base);
    函数说明
    strtoul()会将参数nptr字符串根据参数base来转换成无符号的长整型数。参数base范围从2至36,或0。参数base代表采用的进制 方式,如base值为10则采用10进制,若base值为16则采用16进制数等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符 则会使用16进制做转换。一开始strtoul()会扫描参数nptr字符串,跳过前面的空格字符串,直到遇上数字或正负符号才开始做转换,再遇到非数字 或字符串结束时('')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。
    返回值
    返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。
    附加说明
    ERANGE指定的转换字符串超出合法范围。
    范例
    参考strtol()




    toascii(将整型数转换成合法的ASCII 码字符)
    相关函数
    isascii,toupper,tolower
    表头文件
    #include<ctype.h>
    定义函数
    int toascii(int c)
    函数说明
    toascii()会将参数c转换成7位的unsigned char值,第八位则会被清除,此字符即会被转成ASCII码字符。
    返回值
    将转换成功的ASCII码字符值返回。
    范例
    #include<stdlib.h>
    main()
    {
    int a=217;
    char b;
    printf(“before toascii () : a value =%d(%c) ”,a,a);
    b=toascii(a);
    printf(“after toascii() : a value =%d(%c) ”,b,b);
    }
    执行
    before toascii() : a value =217()
    after toascii() : a value =89(Y)

     这些转换只能在一定范围内,如果数太大可能就失真。

  • 相关阅读:
    platform_device和platform_driver
    理解和认识udev
    platform_device和platform_driver
    bzImage的概要生成过程
    shell 字符表
    分析mtk6516如何加入自己的驱动
    理解和使用Linux的硬件抽象层HAL
    bzImage的概要生成过程
    理解和认识udev
    shell 字符表
  • 原文地址:https://www.cnblogs.com/satan-shanks/p/3847380.html
Copyright © 2011-2022 走看看