C++ 常用宏

常用宏定义

_DOS_       表示MS-DOS 16位系统平台
WIN32       表示Windows32位系统平台
WIN64       表示Windows64位系统平台
_WIN32_WCE  表示Window Mobile 32位系统平台
_UNIX       表示UNIX系统平台
_POSIX_     表示POSIX(Portable Operating System Interface of Unix)系统平台
_LINUX_     表示LINUX系统平台
_APPLE_     表示苹果系统平台
_MAC_苹果   表示苹果系统平台
_ATL_VER    表示ATL版本号
_MSC_VER    表示C++编译器版本号
_MFC_VER    表示MFC版本号
__CLR_VER   表示CLR版本号
_CONSOLE    表示控制台程序
_WINDOWS    表示窗口程序
_DEBUG      表示Debug版本
NDEBUG      表示Release版本
_MBCS       表示使用多字节字符集
_UNICODE    表示使用UNICODE字符集
_WINDLL     表示要做一个用到MFC的DLL
_USRDLL     表示做一个用户DLL(相对MFC扩展DLL而言)
_AFXDLL     表示使用MFC动态链接库
_AFXEXT     表示要做一个MFC扩展DLL
__DATE__    表示编译日期
__TIME__    表示编译时间
__FILE__    表示包含当前程序文件名的路径字符串
__LINE__    表示当前程序代码行号
__STDC__    表示编译标准C
__cplusplus 表示编译标准C++
_CRT_SECURE_NO_WARNINGS   表示禁止显示使用不安全的CRT函数时的警告
_CRT_SECURE_NO_DEPRECATE

2.实用宏

//1.安全删除 new new[]
#ifndef SAFE_DEL_PTR
#define SAFE_DEL_PTR(x) if(x){delete (x); (x)=0;}
#endif
#ifndef SAFE_DEL_ARRAY
#define SAFE_DEL_ARRAY(x) if(x){delete[] (x); (x)=0;}
#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)))

原文地址:https://www.cnblogs.com/qiantz/p/3267076.html