题目:宏#define命令练习(1)
#include<cstdio>#define mul(x,y) x * y //其实就是用前面得部分代替后面得部分,可以看下面 // 思考一下mul(x,y) 这样define会不会有问题 int main(){ printf("%d\n",mul(3,5)); // 直接乘以两个数肯定没问题吧 printf("%d\n",mul(1 + 2, 2 + 3));//这里得答案为什么和上面不一样? // 宏定义就是直接替换,所以这里替换得为 1 + 2 * 2 + 3 = 8; // 要想解决写为 mul(x,y) (x) * (y)即可 }
题目47
题目:宏#define命令练习(2)
#include<cstdio>#define sswap(a,b) {\ int t;\ t = a;\ a = b;\ b = t;\}// 真得不会有人觉得这种写法很怪么 int main(){ int x = 1, y = 2; printf("x is %d y is :%d\n",x,y); sswap(x,y); printf("x is %d y is :%d\n",x,y);}
题目48
题目:宏#define命令练习(3)
#include<cstdio>#define LAG > #define SMA <int main(){ int a = 10, b = 12; if(a LAG b) printf("a > b"); else if(a SMA b) printf("a < b"); else printf("a == b");}
题目49
题目:#if #ifdef和#ifndef得综合应用。
#include<cstdio>#define MAX#define MAX_NUM(x,y) (x > y) ? x : y#define MIN_NUM(x,y) (x > y) ? y : xint main(){ // 看看会输出什么? int x = 10, y = 20; #ifdef MAX printf("max_num is %d\n",MAX_NUM(x,y)); #else printf("min_num is %d\n",MIN_NUM(x,y)); #endif // 注意这里是ifndef #ifndef MIN printf("min_num is %d\n",MIN_NUM(x,y)); #else printf("max_num is %d\n",MAX_NUM(x,y)); #endif //看看undef之后还能不能输出下面得内容 #undef MAX #ifdef MAX printf("2:max_num is %d\n",MAX_NUM(x,y)); #else printf("2:min_num is %d\n",MIN_NUM(x,y)); #endif #define MIN // 下面代码请自己尝试一下ifdef 和 ifndef得应用 }
题目50
题目:#include 得应用练习
注意这里是两个文件
#include<cstdio>// 之前一直在调用得为cstdio#include"a.h"// 这里a.h是我们自己写得一个文件int main(){ int a = 10, b = 100; if(a LAG b) printf("a is bigger than b"); if(a SMA b) printf("a is smaller than b"); if(a EQ b) printf("a equals to b");}
a.h文件:
#define LAG > #define SMA <#define EQ ==