#define 总结

1. 定义简单的常数:定义常量,便于修改(切不可在后面加上分号!)
 #define N 1000
 等效于 const int N = 1000; 但略有不同,define只是简单替换,而不是作为一个量来使用.

2. 定义简单的函数:注意多使用括号

define可以像函数那样接受一些参数,如下:

#define max(x,y) (x)>(y)?(x):(y);

这个定义就将返回两个数中较大的那个,看到了吗?因为这个“函数”没有类型检查, 就好像一个函数模板似的,当然,它绝对没有模板那么安全就是了。可以作为一个简单的模板来使用而已。但是这样做的话存在隐患,例子如下:

#define Add(a,b) a+b;

在一般使用的时候是没有问题的,但是如果遇到如:c * Add(a,b) * d的时候就会出现问题,代数式的本意是a+b然后去和c,d相乘,但是因为使用了define(它只是一个简单的替换),所以式子实际上变成了

c*a + b*d

另外举一个例子:

#define pin (int*);

pin a,b;

本意是a和b都是int型指针,但是实际上变成int* a,b;

a是int型指针,而b是int型变量。

这是应该使用typedef来代替define,这样a和b就都是int型指针 了。

所以我们在定义的时候,养成一个良好的习惯,建议所有的层次都要加括号。


3. 定义单行宏:主要有以下三种用法.
   1) 前加##或后加##,将标记作为一个合法的标识符的一部分.注意,不是字符串.多用于多行的宏定义中.例如:
#define A(x)  T_##x
则 int A(1) = 10; //等效于int T_1 = 10;
#define A(x)  Tx##__
则 int A(1) = 10; //等效于int T1__ = 10;
   2) 前加#@,将标记转换为相应的字符,注意:仅对单一标记转换有效(理解有误?)
 #define B(x) #@x
 则B(a)即’a’,B(1)即’1’.但B(abc)却不甚有效.
   3) 前加#,将标记转换为字符串.
 #define C(x) #x
 则C(1+1) 即 ”1+1”.

4. 定义多行宏:注意斜杠的使用,最后一行不能用斜杠.
 #define DECLARE_RTTI(thisClass, superClass)/
  virtual const char* GetClassName() const/ 
  {return #thisClass;}/
  static int isTypeOf(const char* type)/
  {/
   if(!strcmp(#thisClass, type)/
    return 1;/
   return superClass::isTypeOf(type);/
   return 0;/
  }/
  virtual int isA(const char* type)/
  {/
   return thisClass::isTypeOf(type);/
  }/
  static thisClass* SafeDownCast(DitkObject* o)/
  {/
   if(o&&o->isA(#thisClass))/
    return static_cast<thisClass*>(o);/
   return NULL;/
  }

5. 用于条件编译:(常用形式)
 #ifndef _AAA_H
 #define _AAA_H
 //c/c++代码
 #endif

在大规模的开发过程中,特别是跨平台和系统的软件里,define最重要的功能是 条件编译。就是:

#ifdef WINDOWS
......
......

#endif

#ifdef LINUX
......
......
#endif
可以在编译的时候通过#define设置编译环境

5.如何定义宏、取消宏

//定义宏
#define [MacroName] [MacroValue]

//取消宏
#undef [MacroName]

普通宏
#define PI (3.1415926)

带参数的宏
#define max(a,b) ((a)>(b)? (a),(b))

关键是十分容易产生错误,包括机器和人理解上的差异等等。

 

6.条件编译

#ifdef XXX…(#else) …#endif

例如 #ifdef DV22_AUX_INPUT

#define AUX_MODE 3 

#else

#define AUY_MODE 3

#endif

#ifndef XXX … (#else) … #endif

 

7.头文件(.h)可以被头文件或C文件包含;

重复包含(重复定义)

由于头文件包含可以嵌套,那么C文件就有可能包含多次同一个头文件,就可能出现重复定义的问题的。

通过条件编译开关来避免重复包含(重复定义)

例如

#ifndef __headerfileXXX__

#define __headerfileXXX__

文件内容

#endif 

 

8. 一些注意事项:
  1) 不能重复定义.除非定义完全相同.#define A(x) … 和#define A 是重复定义.

  2) 可以只定义符号,不定义值.如#define AAA

转自http://www.cnblogs.com/phonefans/archive/2009/02/25/1398121.html

 

main.c(20): error C202: 'PWM2T2': undefined identifier main.c(21): error C202: 'PWM3T2': undefined identifier main.c(22): error C202: 'PWM4T2': undefined identifier main.c(23): error C202: 'PWM5T2': undefined identifier main.c(24): error C202: 'PWM6T2': undefined identifier main.c(25): error C202: 'PWM7T2': undefined identifier main.c(37): error C202: 'PWM2T1': undefined identifier main.c(38): error C202: 'PWM2T2': undefined identifier main.c(42): error C202: 'PWMC': undefined identifier代码运行时出现这些问题,应该是头文件的定义和代码中的定义不符合,我给你部分头文件中关于PWM的代码,你看看怎么修改sfr B = 0xF0; sfr PWMCFG = 0xF1; sfr PCA_PWM0 = 0xF2; sfr PCA_PWM1 = 0xF3; sfr PCA_PWM2 = 0xF4; sfr PWMCR = 0xF5; sfr PWMIF = 0xF6; sfr PWMFDCR = 0xF7; sfr P7 = 0xF8; sfr CH = 0xF9; sfr CCAP0H = 0xFA; sfr CCAP1H = 0xFB; sfr CCAP2H = 0xFC; #define PWMCH 0xFFF0 #define PWMCL 0xFFF1 #define PWMCKS 0xFFF2 #define PWM2T1H 0xFF00 #define PWM2T1L 0xFF01 #define PWM2T2H 0xFF02 #define PWM2T2L 0xFF03 #define PWM2CR 0xFF04 #define PWM3T1H 0xFF10 #define PWM3T1L 0xFF11 #define PWM3T2H 0xFF12 #define PWM3T2L 0xFF13 #define PWM3CR 0xFF14 #define PWM4T1H 0xFF20 #define PWM4T1L 0xFF21 #define PWM4T2H 0xFF22 #define PWM4T2L 0xFF23 #define PWM4CR 0xFF24 #define PWM5T1H 0xFF30 #define PWM5T1L 0xFF31 #define PWM5T2H 0xFF32 #define PWM5T2L 0xFF33 #define PWM5CR 0xFF34 #define PWM6T1H 0xFF40 #define PWM6T1L 0xFF41 #define PWM6T2H 0xFF42 #define PWM6T2L 0xFF43 #define PWM6CR 0xFF44 #define PWM7T1H 0xFF50 #define PWM7T1L 0xFF51 #define PWM7T2H 0xFF52 #define PWM7T2L 0xFF53 #define PWM7CR 0xFF54 #define PWM2_ID 0 #define PWM3_ID 1 #define PWM4_ID 2 #define PWM5_ID 3 #define PWM6_ID 4 #define PWM7_ID 5 #define PwmClk_1T 0 #define PwmClk_2T 1 #define PwmClk_3T 2 #define PwmClk_4T 3 #define PwmClk_5T 4 #define PwmClk_6T 5 #define PwmClk_7T 6 #define PwmClk_8T 7 #define PwmClk_9T 8 #define PwmClk_10T 9 #define PwmClk_11T 10 #define PwmClk_12T 11 #define PwmClk_13T 12 #define PwmClk_14T 13 #define PwmClk_15T 14 #define PwmClk_16T 15 #define PwmClk_T2 16 #define PWMn_PS_1 0x00 #define PWMn_PS_2 0x08 #define EPWMnI_Enable 0x04 #define ECnT2SI_Enable 0x02 #define ECnT1SI_Enable 0x01 #define PWM2_P37 0x00 #define PWM2_P27 0x08 #define PWM3_P21 0x00 #define PWM3_P45 0x08 #define PWM4_P22 0x00 #define PWM4_P44 0x08 #define PWM5_P23 0x00 #define PWM5_P42 0x08 #define PWM6_P16 0x00 #define PWM6_P07 0x08 #define PWM7_P17 0x00 #define PWM7_P06 0x08 #define EAXSFR() P_SW2 |= 0x80 #define EAXRAM() P_SW2 &= ~0x80 #define C2INI 0x01 #define C3INI 0x02 #define C4INI 0x04 #define C5INI 0x08 #define C6INI 0x10 #define C7INI 0x20 #define CBTADC 0x40 #define ENPWM 0x80 #define ECBI 0x40 #define ENC7O 0x20 #define ENC6O 0x10 #define ENC5O 0x08 #define ENC4O 0x04 #define ENC3O 0x02 #define ENC2O 0x01 #define PWM_Enable() PWMCR |= 0x80 #define PWM_Disable() PWMCR &= ~0x80 #define CBIF 0x40 #define C7IF 0x20 #define C6IF 0x10 #define C5IF 0x08 #define C4IF 0x04 #define C3IF 0x02 #define C2IF 0x01 #define ENFD 0x20 #define FLTFLIO 0x10 #define EFDI 0x08 #define FDCMP 0x04 #define FDIO 0x02 #define FDIF 0x01 #define PWM_FaultDetect_Enable() PWMFDCR |= 0x20 #define PWM_FaultDetect_Disable() PWMFDCR &= ~0x20
最新发布
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值