C语言常用数据类型和自定义数据类型的存储空间
想知道各种数据类型的存储空间长度,编码测试,代码如下:
- 注意:用到offsetof宏,在stddef.h头文件中定义,一定要包含进去!!!
/* filename: sizeof.c */
#include <stdio.h>
#include <stddef.h> //for offsetof macro/* compile : gcc sizeof.c -o sizeofrun : ./sizeof *//* output sizeof common datatype and pointer */
void
out_sizeof (void)
{printf ("Sizeof CHAR : %lu\n", sizeof(char));printf ("Sizeof SHORT : %lu\n", sizeof(short));printf ("Sizeof INT : %lu\n", sizeof(int));printf ("Sizeof LONG : %lu\n", sizeof(long));printf ("Sizeof LONG LONG : %lu\n", sizeof(long long));printf ("Sizeof FLOAT : %lu\n", sizeof(float));printf ("Sizeof DOUBLE : %lu\n", sizeof(double));printf ("Sizeof LONG DOUBLE : %lu\n", sizeof(long double));printf ("Sizeof VOID : %lu\n", sizeof(void));printf ("---------------------------------------------\n");printf ("Sizeof CHAR pointer : %lu\n", sizeof(char*));printf ("Sizeof SHORT pointer : %lu\n", sizeof(short*));printf ("Sizeof INT pointer : %lu\n", sizeof(int*));printf ("Sizeof LONG pointer : %lu\n", sizeof(long*));printf ("Sizeof FLOAT pointer : %lu\n", sizeof(float*));printf ("Sizeof DOUBLE pointer : %lu\n", sizeof(double*));printf ("Sizeof VOID pointer : %lu\n", sizeof(void*));printf ("---------------------------------------------\n");
}/* define a function pointer */
typedef void (*MyFunctionPointer) (void);/* define a struct */
typedef struct _Obj Obj;
struct _Obj
{int id;char name[12];int *p;
};/* define a enum */
enum dtype {DT_INT, DT_FLOAT, DT_CHAR
};/* define a union */
typedef union _Uval Uval;
union _Uval
{double fval;long lval;char *sval;
};/* define a function */
void
out_info (void)
{printf ("Some information here!\n");
}/* output sizeof updefine datatype and other */
void
out_sizeof_other (void)
{char c[20];char buf[8][8];printf ("Sizeof struct _Obj is %lu\n", sizeof(struct _Obj));printf ("The name of struct _Obj offset is %lu\n",offsetof(Obj, name));printf ("The p of struct _Obj offset is %lu\n",offsetof(Obj, p));printf ("------------------------------------------------\n");printf ("Sizeof enum dtype is %lu\n", sizeof(enum dtype));printf ("Sizeof union Uval is %lu\n", sizeof(Uval));printf ("Sizeof function pointer is %lu\n", sizeof (MyFunctionPointer));printf ("Sizeof FUNCTION : %lu\n", sizeof(out_info));printf ("Sizeof char array c[20] is %lu\n", sizeof(c));printf ("Sizeof char array buf[8][8] is %lu\n", sizeof(buf));printf ("------------------------------------------------\n");
}/**/
int
main (int argc, char *argv[])
{out_sizeof ();out_sizeof_other ();return 0;
}
/* --(:-:)-- */
编译运行,结果如下:
songvm@ubuntu:~/works/xdn$ gcc sizeof.c -o sizeof
songvm@ubuntu:~/works/xdn$ ./sizeof
Sizeof CHAR : 1
Sizeof SHORT : 2
Sizeof INT : 4
Sizeof LONG : 8
Sizeof LONG LONG : 8
Sizeof FLOAT : 4
Sizeof DOUBLE : 8
Sizeof LONG DOUBLE : 16
Sizeof VOID : 1
---------------------------------------------
Sizeof CHAR pointer : 8
Sizeof SHORT pointer : 8
Sizeof INT pointer : 8
Sizeof LONG pointer : 8
Sizeof FLOAT pointer : 8
Sizeof DOUBLE pointer : 8
Sizeof VOID pointer : 8
---------------------------------------------
Sizeof struct _Obj is 24
The name of struct _Obj offset is 4
The p of struct _Obj offset is 16
------------------------------------------------
Sizeof enum dtype is 4
Sizeof union Uval is 8
Sizeof function pointer is 8
Sizeof FUNCTION : 1
Sizeof char array c[20] is 20
Sizeof char array buf[8][8] is 64
------------------------------------------------
常用数据类型、指针、结构体、共用体、枚举
- long long 长度为8字节,与long长度相同,可见在LINUX平台用long long不是必要选项!!!
- 单精度(float)、双精度(double)、长双精度(long double0的长度分别为4、8、16字节,即32位、64位、128位,位数决定运算精度!!!
- 基本类型的指针类型长度都为8字节!!!
- 结构体的长度取决于用户的定义的成员的长度的和!!!
- 共用体的长度等于最长的成员长度!!!
- 枚举类型的存储长度与整型int相同!!!
定义了函数指针、结构体、枚举、共用体、一个简单的函数
- 函数指针定义:
注意:按需求写出返回类型和参数,无类型用void,名字前加星号再用圆括号包上!!!
typedef void (*MyFunctionPointer) (void);
- 结构体定义:
注意:offsetof宏做为一个值使用,指出结构成员在结构体中的偏移数值!!!
typedef struct _Obj Obj;
struct _Obj
{int id; // 4 byteschar name[12]; // 12 bytesint *p; // 8 bytes
}; //---------// 24 bytes
- 枚举类型定义:
同样也可以用typedef来定义,可以直接引用DT_INT, DT_FLOAT, DT_CHAR这三个值,提高了代码的可读性!!!
enum dtype {DT_INT, DT_FLOAT, DT_CHAR
};
- 共用体定义:
共用体的三个成员共用同一个存储空间,长度由占用空间最大的成员来决定!!!
下面的定义加了一个整型的ival,长度是4bytes,其他成员长度是8bytes,所以最终长度还是8bytes。
typedef union _Uval Uval;
union _Uval
{int ival; // 4 bytesdouble fval; // 8 byteslong lval; // 8 byteschar *sval; // 8 bytes
}; //-----------// 8 bytes
- 函数定义:
/* define a simple function */
void
out_info (void)
{printf ("Some information here!\n");
}
- C语言中的函数是最好玩的,下一步就研究玩函数!!!