cgg之数据类型

时间:2022-09-03 06:36:16

所有例子都在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 ==
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 (-)
# define INT16_MIN (--)
# define INT32_MIN (--)
# define INT64_MIN (-__INT64_C()-)
/* Maximum of signed integral types. */
# define INT8_MAX ()
# define INT16_MAX ()
# define INT32_MAX ()
# define INT64_MAX (__INT64_C())

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

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

输出结果为:

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

可以看出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\n",sizeof(int));
printf("%d\n",sizeof(long));

输出结果为:

4
8

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

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

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

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

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

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

 1.2浮点数

c提供不同精度的浮点:

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

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

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

测试结果:

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

输出:

16
8
4

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

对应的文件为bits/mathdef.h

# if __WORDSIZE ==  || (defined __FLT_EVAL_METHOD__ && __FLT_EVAL_METHOD__ == )
/* 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\n",creal(comp));
printf("%f\n",cimag(comp));

输出:

1.000000
3.000000

1.3枚举

例子

    enum color {black,red=,green};

    enum color b = black;

    enum color r = red;
enum color g = green; printf("black=%d\n",b);
printf("red=%d\n",r);
printf("green=%d\n",g);

输出结果为:

black=
red=
green=

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

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

    enum color {black,red=,green=};

    enum color b = black;

    enum color r = red;
enum color g = green; printf("black=%d\n",b);
printf("red=%d\n",r);
printf("green=%d\n",g);

输出为:

black=
red=
green=

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

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

输出:

[root@typhoeus79 ]# ./test_enum 

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