实现代码实例
程序代码:
[cpp] view plaincopyprint #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
/***** cplusplus *****/
#if 0
#include <iostream>
using namespace std;
#endif
// 得到指定地址上的一个字节或字
#define MEM_B(x) (*((byte *)(x)))
#define MEM_W(x) (*((word *)(x)))
// 得到一个field在结构体(struct)中的偏移量
#define FPOS(type, field) ((dword)&((type *)0)->field)
// 将一个字母字符转换为大写
#define UPCASE(c) (((c)>='a' && (c)<='z') ((c)-0x20) : (c))
// 判断字符是否为十进制的数字
#define DECCHECK(c) ((c)>='0' && (c)<='9')
// 判断字符是否为十六进制的数字
#define HEXCHECK(hex) (((hex)>='0' && (hex)<='9')||((hex)>='A' && (hex)<='F')||((hex)>='a' && (hex)<='f'))
// 防止溢出的一个方法
#define INC_SAT(val) (val=((val)+1 > (val)) (val)+1 : (val))
// 计算数组元素的个数
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
int
main(void)
{
int x = 0x1234abcd;
char c = 'a';
char dec = '5';
char hex = 'e';
char array = {'1'};
//printf("MEM_B(x): 0x%p/n", MEM_B(x));
//printf("MEM_W(x): 0x%p/n", MEM_W(x));
printf("UPCASE(c): %c -> %c/n", c, UPCASE(c));
printf("DECCHECK(dec): %c -> %d/n", dec, DECCHECK(dec));
printf("HEXCHECK(hex): %c -> %d/n", hex, HEXCHECK(hex));
printf("ARRAY_SIZE(array): array -> %d/n", ARRAY_SIZE(array));
printf("/n/****** MACRO ******//n");
printf("__LINE__: %d/n", __LINE__);
printf("__FILE__: %s/n", __FILE__);
printf("__DATE__: %s/n", __DATE__);
printf("__TIME__: %s/n", __TIME__);
printf("__func__: %s/n", __func__);
#ifdef __cplusplus
cout 《"hello __cplusplus"《endl;
#endif
#ifdef __STDC__
printf("hello __STDC__/n");
#endif
printf("/n/****** sizeof() ******//n");
//printf("sizeof(byte): %d/n", sizeof(byte));
printf("sizeof(char): %d/n", sizeof(char));
printf("sizeof(signed char): %d/n", sizeof(signed char));
printf("sizeof(unsigned char): %d/n", sizeof(unsigned char));
printf("sizeof(short): %d/n", sizeof(short));
printf("sizeof(signed short): %d/n", sizeof(signed short));
printf("sizeof(unsigned short): %d/n", sizeof(unsigned short));
printf("sizeof(int): %d/n", sizeof(int));
printf("sizeof(signed int): %d/n", sizeof(signed int));
printf("sizeof(unsigned int): %d/n", sizeof(unsigned int));
printf("sizeof(long): %d/n", sizeof(long));
printf("sizeof(signed long): %d/n", sizeof(signed long));
printf("sizeof(unsigned long): %d/n", sizeof(unsigned long));
printf("sizeof(long long): %d/n", sizeof(long long));
printf("sizeof(signed long long): %d/n", sizeof(signed long long));
printf("sizeof(unsigned long long): %d/n", sizeof(unsigned long long));
printf("sizeof(float): %d/n", sizeof(float));
printf("sizeof(double): %d/n", sizeof(double));
exit(EXIT_SUCCESS);
}
运行结果:
[work@db-testing-com06-vm3.db01.baidu.com c++]$ gcc -W -o micro micro.c
[work@db-testing-com06-vm3.db01.baidu.com c++]$ ./micro
UPCASE(c): a -> A
DECCHECK(dec): 5 -> 1
HEXCHECK(hex): e -> 1
ARRAY_SIZE(array): array -> 10
/****** MACRO ******/
__LINE__: 50
__FILE__: micro.c
__DATE__: Dec 28 2010
__TIME__: 19:25:10
__func__: main
hello __STDC__
/****** sizeof() ******/
sizeof(char): 1
sizeof(signed char): 1
sizeof(unsigned char): 1
sizeof(short): 2
sizeof(signed short): 2
sizeof(unsigned short): 2
sizeof(int): 4
sizeof(signed int): 4
sizeof(unsigned int): 4
sizeof(long): 8
sizeof(signed long): 8
sizeof(unsigned long): 8
sizeof(long long): 8
sizeof(signed long long): 8
sizeof(unsigned long long): 8
sizeof(float): 4
sizeof(double): 8
==========================================
C宏定义的简单总结
1,防止一个头文件被重复包含
#ifndef BODYDEF_H
#define BODYDEF_H
//头文件内容
#endif
2,得到指定地址上的一个字节或字
#define MEM_B( x ) ( *( (byte *) (x) ) )
#define MEM_W( x ) ( *( (word *) (x) ) )
3,得到一个field在结构体(struct)中的偏移量
#define FPOS( type, field ) ( (dword) &(( type *) 0)-> field )
4,得到一个结构体中field所占用的字节数
#define FSIZ( type, field ) sizeof( ((type *) 0)->field )
5,得到一个变量的地址(word宽度)
#define B_PTR( var ) ( (byte *) (void *) &(var) )
#define W_PTR( var ) ( (word *) (void *) &(var) )
6,将一个字母转换为大写
#define UPCASE( c ) ( ((c) >= ''a'' && (c) <= ''z'') ((c) - 0x20) : (c) )
7,判断字符是不是10进值的数字
#define DECCHK( c ) ((c) >= ''0'' && (c) <= ''9'')
8,判断字符是不是16进值的数字
#define HEXCHK( c ) ( ((c) >= ''0'' && (c) <= ''9'') ||((c) >= ''A'' && (c) <= ''F'') ||((c) >= ''a'' && (c) <= ''f'') )
9,防止溢出的一个方法
#define INC_SAT( val ) (val = ((val)+1 > (val)) (val)+1 : (val))
10,返回数组元素的个数
#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )
11,使用一些宏跟踪调试
ANSI标准说明了五个预定义的宏名。它们是:
_LINE_ (两个下划线),对应%d
_FILE_ 对应%s
_DATE_ 对应%s
_TIME_ 对应%s
_STDC_
宏中"#"和"##"的用法
我们使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起。
#define STR(s) #s
#define CONS(a,b) int(a##e##b)
Printf(STR(vck)); // 输出字符串"vck"
printf("%d/n", CONS(2,3)); // 2e3 输出:2000