實現代碼實例
程序代碼:
#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[10] = {'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[10] -> %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); }
運行結果:
[[email protected] c++]$ gcc -W -o micro micro.c
[[email protected] c++]$ ./micro
UPCASE(c): a -> A
DECCHECK(dec): 5 - > 1
HEXCHECK(hex): e -> 1
ARRAY_SIZE(array): array[10] -> 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_
#b)
printf ("%s/n", CONS(A, A)); // compile error
這一行則是:
printf("%s/n", int(AeA));
INT_MAX和A都不會再被展開, 然而解決這個問題的方法很簡單. 加多一層中間轉 換宏.
加這層宏的用意是把所有宏的參數在這層裡全部展開, 那麼在轉換宏裡的那一個宏(_STR)就能得到正確的宏參數
#define STR(s) _STR(s) // 轉換宏
#define CONS(a,b) _CONS(a,b) // 轉換宏
printf("int max: %s/n", STR(INT_MAX)); // INT_MAX,int型的最大值,為一個變量 #include<climits>
輸出為: int max: 0x7fffffff
STR(INT_MAX) --> _STR(0x7fffffff) 然後再轉換成字符串;
printf("%d/n", CONS (A, A));
輸出為:200
CONS(A, A) --> _CONS((2), (2)) --> int((2)e(2))
"#"和"##"的一些應用特例
1、合並匿名變量名
#define ___ANONYMOUS1(type, var, line) type var##line
#define __ANONYMOUS0(type, line) ___ANONYMOUS1(type, _anonymous, line)
#define ANONYMOUS(type) __ANONYMOUS0(type, __LINE__)
例:ANONYMOUS(static int); 即: static int _anonymous70; 70表示該行行號;
第一層:ANONYMOUS(static int); --> __ANONYMOUS0 (static int, __LINE__);
第二層: --> ___ANONYMOUS1(static int, _anonymous, 70);
第三層: --> static int _anonymous70;
即每次只能解開當前 層的宏,所以__LINE__在第二層才能被解開;
2、填充結構
#define FILL(a) {a, #a}
enum IDD {OPEN, CLOSE};
typedef struct MSG{
IDD id;
const char * msg;
}MSG;
MSG _msg[] = {FILL(OPEN), FILL(CLOSE)};
相當於:
MSG _msg[] = {{OPEN, "OPEN"},
{CLOSE, "CLOSE"}};
3、記錄文件名
#define _GET_FILE_NAME(f) #f
#define GET_FILE_NAME(f) _GET_FILE_NAME(f)
static char FILE_NAME[] = GET_FILE_NAME(__FILE__);
4、得到一個數值類型所對應的字符串緩沖大小
#define _TYPE_BUF_SIZE(type) sizeof #type
#define TYPE_BUF_SIZE(type) _TYPE_BUF_SIZE(type)
char buf [TYPE_BUF_SIZE(INT_MAX)];
--> char buf[_TYPE_BUF_SIZE(0x7fffffff)];
--> char buf[sizeof "0x7fffffff"];
這裡相當於:
char buf[11];
==========================================
C宏定義的技巧總結
1,防止一個頭文件被重復包含
#ifndef COMDEF_H
#define COMDEF_H
//頭文件內容
#endif
2,重新定義一些類型,防止由於 各種平台和編譯器的不同,而產生的類型字節數差異,方便移植。
typedef unsigned char boolean; /* Boolean value type. */
typedef unsigned long int uint32; /* Unsigned 32 bit value */
typedef unsigned short uint16; /* Unsigned 16 bit value */
typedef unsigned char uint8; /* Unsigned 8 bit value */
typedef signed long int int32; /* Signed 32 bit value */
typedef signed short int16; /* Signed 16 bit value */
typedef signed char int8; /* Signed 8 bit value */
//下面的不建議使用
typedef unsigned char byte; /* Unsigned 8 bit value type. */
typedef unsigned short word; /* Unsinged 16 bit value type. */
typedef unsigned long dword; /* Unsigned 32 bit value type. */
typedef unsigned char uint1; /* Unsigned 8 bit value type. */
typedef unsigned short uint2; /* Unsigned 16 bit value type. */
typedef unsigned long uint4; /* Unsigned 32 bit value type. */
typedef signed char int1; /* Signed 8 bit value type. */
typedef signed short int2; /* Signed 16 bit value type. */
typedef long int int4; /* Signed 32 bit value type. */
typedef signed long sint31; /* Signed 32 bit value */
typedef signed short sint15; /* Signed 16 bit value */
typedef signed char sint7; /* Signed 8 bit value */
3,得到指定地址上的一個字節或字
#define MEM_B( x ) ( *( (byte *) (x) ) )
#define MEM_W( x ) ( *( (word *) (x) ) )
4,求最大值和最小值
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
5,得到一個field在結構體 (struct)中的偏移量
#define FPOS( type, field ) /
/*lint -e545 */ ( (dword) &(( type *) 0)-> field ) /*lint +e545 */
6,得到一個結構體中field所占用的字節數
#define FSIZ( type, field ) sizeof( ((type *) 0)->field )
7,按照LSB格式把兩個字節轉化為一個Word
#define FLIPW( ray ) ( (((word) (ray)[0]) * 256) + (ray)[1] )
8,按照LSB格式把一個Word轉化為兩個字節
#define FLOPW( ray, val ) /
(ray)[0] = ((val) / 256); /
(ray)[1] = ((val) & 0xFF)
9,得到一個變量的地址(word寬度)
#define B_PTR( var ) ( (byte *) (void *) &(var) )
#define W_PTR( var ) ( (word *) (void *) &(var) )
10,得到一個字的高位和低位字節
#define WORD_LO(xxx) ((byte) ((word)(xxx) & 255))
#define WORD_HI(xxx) ((byte) ((word)(xxx) >> 8))
11,返回一個比X大的最接近的8的倍數
#define RND8( x ) ((((x) + 7) / 8 ) * 8 )
12,將一個字母轉換為大寫
#define UPCASE( c ) ( ((c) >= ''a'' && (c) <= ''z'') ? ((c) - 0x20) : (c) )
13,判斷字符是不是10進值的數字
#define DECCHK( c ) ((c) >= ''0'' && (c) <= ''9'')
14,判斷字符是不是16進值的數字
#define HEXCHK( c ) ( ((c) >= ''0'' && (c) <= ''9'') ||/
((c) >= ''A'' && (c) <= ''F'') ||/
((c) >= ''a'' && (c) <= ''f'') )
15,防止溢出的一個方法
#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val))
16,返回數組元素的個數
#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )
17,返回一個 無符號數n尾的值MOD_BY_POWER_OF_TWO(X,n)=X%(2^n)
#define MOD_BY_POWER_OF_TWO( val, mod_by ) /
( (dword)(val) & (dword)((mod_by)-1) )
18,對於IO空間映射在存儲空間的結構,輸入輸出 處理
#define inp(port) (*((volatile byte *) (port)))
#define inpw(port) (*((volatile word *) (port)))
#define inpdw(port) (* ((volatile dword *)(port)))
#define outp(port, val) (*((volatile byte *) (port)) = ((byte) (val)))
#define outpw(port, val) (*((volatile word *) (port)) = ((word) (val)))
#define outpdw(port, val) (*((volatile dword *) (port)) = ((dword) (val)))
#b)
int main()
{
printf(STR(vck)); // 輸出字符串"vck"
printf("%d/n", CONS(2,3)); // 2e3 輸出:2000
return 0;
}
#b)
#define CONS(a,b) _CONS(a,b) // 轉換宏
printf("int max: %s/n", STR(INT_MAX)); // INT_MAX,int型 的最大值,為一個變量 #include<climits>
輸出為: int max: 0x7fffffff
STR(INT_MAX) --> _STR(0x7fffffff) 然後再轉換成字符串;
printf("%d/n", CONS(A, A));
輸出為:200
CONS(A, A) --> _CONS((2), (2)) --> int((2)e(2))
三、''#''和''##''的一些應用特例
1、合並匿名變量 名
#define ___ANONYMOUS1(type, var, line) type var##line
#define __ANONYMOUS0(type, line) ___ANONYMOUS1 (type, _anonymous, line)
#define ANONYMOUS(type) __ANONYMOUS0(type, __LINE__)
例:ANONYMOUS(static int); 即: static int _anonymous70; 70表示該行行號;
第一層:ANONYMOUS(static int); --> __ANONYMOUS0(static int, __LINE__);
第二層: --> ___ANONYMOUS1(static int, _anonymous, 70);
第三層: --> static int _anonymous70;
即每次只能解開當前層的宏,所以__LINE__在第二層才能被 解開;
2、填充結構
#define FILL(a) {a, #a}
enum IDD{OPEN, CLOSE};
typedef struct MSG {
IDD id;
const char * msg;
}MSG;
MSG _msg[] = {FILL(OPEN), FILL(CLOSE)};
相當於:
MSG _msg[] = {{OPEN, "OPEN"},
{CLOSE, "CLOSE"}};
3、記錄 文件名
#define _GET_FILE_NAME(f) #f
#define GET_FILE_NAME(f) _GET_FILE_NAME(f)
static char FILE_NAME[] = GET_FILE_NAME(__FILE__);
4、得到一個數值類型所對應的字符串緩沖大小
#define _TYPE_BUF_SIZE(type) sizeof #type
#define TYPE_BUF_SIZE(type) _TYPE_BUF_SIZE(type)
char buf [TYPE_BUF_SIZE(INT_MAX)];
--> char buf[_TYPE_BUF_SIZE(0x7fffffff)];
--> char buf[sizeof "0x7fffffff"];
這裡相當於:
char buf[11];