在学校做比赛,查找寻迹小车的资料,看到了一篇51开发的小车文章,那个博主的程序中写到
- #define left_go {Left_Motor_P1 = 1;Left_Motor_P2 = 0;}
- #define left_back {Left_Motor_P1 = 0;Left_Motor_P2 = 1;}
- #define left_stop {Left_Motor_P1 = 1;Left_Motor_P2 = 1;}
- #define right_go {Right_Motor_P1 = 1;Right_Motor_P2 = 0;}
- #define right_back {Right_Motor_P1 = 0;Right_Motor_P2 = 1;}
- #define right_stop {Right_Motor_P1 = 1;Right_Motor_P2 = 1;}
就很好奇,为什么#define可以有{}
当时脑子比较笨,总在查为什么会有大括号,多方查找之下终于找到了突破口《C语言中#define用法总结》
典型的使用方法:
使用宏定义我们可以自己根据自己的习惯来定义甚至改变C语言的语法习惯,例如:
#define BEGIN {
#define END }
int main()BEGIN
printf ("DEFINE----\n");
END
定义一个循环
#define LOOP for(;;)
重新定义数据类型
#define IT int
4.define的多行定义
define可以替代多行的代码,在每一个换行的时候加上一个"\"
#define MAX(X,Y) do { \
语句1; \
语句2; \
/* 注释的写法 */ \
} while(0) /* (no trailing ; ) */ \
然后看懂了,是定义了一段赋值的语句
《寻迹小车开发日记》:https://blog.csdn.net/m0_37655357/article/details/78657918
《C语言中#define用法总结》http://blog.chinaunix.net/uid-17188120-id-4026378.html