• cgg之数据类型


    所有例子都在64为操作系统

    Linux 2.6.30 x86_64 x86_64 x86_64 GNU/Linux

    1.1整数

    在stdint.h中定义一些看上去更明确的整数类型

    #ifndef __int8_t_defined
    # define __int8_t_defined
    typedef signed char     int8_t;
    typedef short int       int16_t;
    typedef int         int32_t;
    # if __WORDSIZE == 64                                                                                                                                        
    typedef long int        int64_t;
    # else
    __extension__
    typedef long long int       int64_t;
    # endif
    #endif
    
    /* Unsigned.  */
    typedef unsigned char       uint8_t;
    typedef unsigned short int  uint16_t;
    #ifndef __uint32_t_defined
    typedef unsigned int        uint32_t;
    # define __uint32_t_defined
    #endif
    #if __WORDSIZE == 64
    typedef unsigned long int   uint64_t;
    #else
    __extension__
    typedef unsigned long long int  uint64_t;
    #endif

    还有各种整数类型的大小限制:

    /* Minimum of signed integral types.  */
    # define INT8_MIN       (-128)
    # define INT16_MIN      (-32767-1)
    # define INT32_MIN      (-2147483647-1)
    # define INT64_MIN      (-__INT64_C(9223372036854775807)-1)
    /* Maximum of signed integral types.  */
    # define INT8_MAX       (127)
    # define INT16_MAX      (32767)
    # define INT32_MAX      (2147483647)
    # define INT64_MAX      (__INT64_C(9223372036854775807))

    字符常量默认是一个int整数,但编译器可以自行决定将其解释为char或者int

        char c = 'a';
        printf("%c,size(char)=%d,size('a')=%d;
    ",c,sizeof(c),sizeof('a'));

    输出结果为:

    a,size(char)=1,size('a')=4;

    可以看出sizeof(c)和sizeof('a')的大小不同,后者为int类型,前者为char

    指针是个有特殊用途的整数,在stdint.h中同样给出其类型定义:

    /* Types for `void *' pointers.  */                                                                                                                          
    #if __WORDSIZE == 64
    # ifndef __intptr_t_defined
    typedef long int        intptr_t;
    #  define __intptr_t_defined
    # endif
    typedef unsigned long int   uintptr_t;
    #else
    # ifndef __intptr_t_defined
    typedef int         intptr_t;
    #  define __intptr_t_defined
    # endif
    typedef unsigned int        uintptr_t;
    #endif

    在64位系统中:

        printf("%d
    ",sizeof(int));
        printf("%d
    ",sizeof(long));

    输出结果为:

    4
    8

    stdint.h中定义了一些辅助宏:

    # define INT8_C(c)  c
    # define INT16_C(c) c
    # define INT32_C(c) c
    # if __WORDSIZE == 64
    #  define INT64_C(c)    c ## L
    # else
    #  define INT64_C(c)    c ## LL                                                                                                                              
    # endif

    对于宏定义中"##"表示什么意思呢?

    http://blog.csdn.net/dotphoenix/article/details/4345174

    表示把左和右结合在一起,作为一个符号,如下举个例子:

    [root@typhoeus79 20131105]# more test.c 
    #include <stdio.h>
    #include <stdint.h>
    
    # define TEST(c) c ## UL
    
    int main()
    {
        printf("%d
    ",sizeof(20));
        printf("%d
    ",sizeof(TEST(20))); //20默认是属于int类似,使用TEST宏之后将其转换为UL,无符号长整型
    
        return 0;
    }
    [root@typhoeus79 20131105]# ./test
    4
    8

     1.2浮点数

    c提供不同精度的浮点:

    *float:32位4字节浮点数,精确度为6

    *double:64位8字节浮点数,精确度为15

    *long double:80位10字节浮点数,精确度为19位

    测试结果:

        printf("%d
    ",sizeof(long double));
        printf("%d
    ",sizeof(double));
        printf("%d
    ",sizeof(float));

    输出:

    16
    8
    4

    对于long double为啥不是10个字节呢??

    对应的文件为bits/mathdef.h

    # if __WORDSIZE == 64 || (defined __FLT_EVAL_METHOD__ && __FLT_EVAL_METHOD__ == 0)
    /* The x86-64 architecture computes values with the precission of the
       used type.  Similarly for -m32 -mfpmath=sse.  */
    typedef float float_t;      /* `float' expressions are evaluated as `float'.  */
    typedef double double_t;    /* `double' expressions are evaluated
                       as `double'.  */
    # else
    /* The ix87 FPUs evaluate all values in the 80 bit floating-point format                                                                                     
       which is also available for the user as `long double'.  Therefore we
       define:  */
    typedef long double float_t;    /* `float' expressions are evaluated as
                       `long double'.  */
    typedef long double double_t;   /* `double' expressions are evaluated as
                       `long double'.  */
    # endif

    c99支持复数,在complex.h中有定义

        double complex comp = 1.0 +3.0 * I;
    
        printf("%f
    ",creal(comp));
        printf("%f
    ",cimag(comp));

    输出:

    1.000000
    3.000000

    1.3枚举

    例子

        enum color {black,red=5,green};
    
        enum color b = black;
    
        enum color r = red;
        enum color g = green;
    
        printf("black=%d
    ",b);
        printf("red=%d
    ",r);
        printf("green=%d
    ",g);

    输出结果为:

    black=0
    red=5
    green=6

    默认是递增,从0开始,若中间有重新设置,如例子中red=5,后面的还是继续加1

    枚举成员的值是可以相同的,这样有什么用呢??

        enum color {black,red=5,green=5};
    
        enum color b = black;
    
        enum color r = red;
        enum color g = green;
    
        printf("black=%d
    ",b);
        printf("red=%d
    ",r);
        printf("green=%d
    ",g);

    输出为:

    black=0
    red=5
    green=5

    通常省略枚举小标签用来代替宏定义常量

    [root@typhoeus79 20131105]# more test_enum.c 
    #include <stdio.h>
    
    int main()
    {
        enum {BLACK=0,RED=5,GREEN,YELLOW=5};
    
        printf("%d
    ",BLACK);
        printf("%d
    ",RED);
        printf("%d
    ",GREEN);
        printf("%d
    ",YELLOW);
    }

    输出:

    [root@typhoeus79 20131105]# ./test_enum 
    0
    5
    6
    5

    可以作为定义一些常量使用。

  • 相关阅读:
    JavaScript:综合案例---房贷计算器的实现
    iOS:如何将自己的SDK用CocoaPods管理
    JavaScript:综合案例-表单验证
    JavaScript:日期选择器组件的使用
    JavaScript : 基本的处理事件
    JavaScript:window窗口对象
    JavaScript:文本域事件处理
    JavaScript:下拉列表框的事件处理
    JavaScript:复选框事件的处理
    JavaScript:单选钮的事件处理
  • 原文地址:https://www.cnblogs.com/gsblog/p/3409335.html
Copyright © 2020-2023  润新知