#define代码集合

  1 /**
  2  *  文  件:Definesing.cpp
  3  *  功  能:宏定义的实例集合!
  4  *  作  者:rainy_blue_sky
  5  *  主  页:http://hi.baidu.com/rainy_blue_sky
  6  *  说  明:请尊重知识产权,转载请勿删掉该注释!
  7  */
  8 #include <iostream>
  9 #include <fstream>
 10 #include <iomanip>
 11 using namespace std;
 12 
 13 
 14 int main()
 15 {
 16     //ofstream cout("1.txt");
 17 
 18     cout << "******特殊的define用法******\n";
 19     #define A1(a) T_##a     //A1(1) --> T_1
 20     #define A2(a) @a        //A2(1) --> '1'
 21     #define A3(a) #a        //A3(1) --> "1"
 22     #define T_1 10
 23     cout << A1(1) << endl;
 24 //    cout << A2(1) << endl;        //无法编译通过
 25     cout << A3(1) << endl;
 26 
 27     cout << "\n******define的多行定义******\n";
 28     #define MACRO(arg1, arg2) do{\
 29     /*declarations*/\
 30     int a;\
 31     int b;\
 32     a = arg1;\
 33     b = arg2;\
 34     cout << a << "\t" << b << endl;\
 35     /*other statement*/\
 36     }while(0)
 37     MACRO(10, 20);
 38 
 39     cout << "\n******如何定义宏、取消宏******\n";
 40     #define PI (3.1415926)
 41     #undef PI
 42     cout << "#define [MacroName] [MacroValue]\n"
 43          << "#unde [MacroName]\n";
 44 
 45     cout << "\n******条件编译******\n";
 46     #ifdef SJ
 47     cout << "SJ is Define" << endl;
 48     #else
 49     cout << "SJ is not Define" << endl;
 50     #endif // SJ
 51 
 52     cout << "\n******重新定义类型******\n";
 53     typedef unsigned char boolean;
 54     typedef unsigned long int uint32;
 55     typedef unsigned short uint16;
 56     typedef unsigned char uint8;
 57     typedef signed long int int32;
 58     typedef signed short int16;
 59     typedef signed char int8;
 60     cout << "typedef unsigned char boolean;\n"
 61          << "typedef unsigned long int uint32;\n"
 62          << "typedef unsigned short uint16;\n"
 63          << "typedef unsigned char uint8;\n"
 64          << "typedef signed long int int32;\n"
 65          << "typedef signed short int16;\n"
 66          << "typedef signed char int8;\n";
 67     unsigned char a;
 68     a = 'c';
 69 
 70     cout << "\n******得到指定地址上的一个字节或者字******\n";
 71     typedef unsigned char byte;
 72     typedef unsigned short word;
 73     #define GET_BYTE(x) (*((byte*)(x)))
 74     #define GET_WORD(x) (*((word*)(x)))
 75     int address = 0;
 76     setiosflags(ios::uppercase);
 77     cout << GET_BYTE(&a) << endl
 78          << GET_WORD(&a) << endl;
 79 
 80     cout << "\n******得到一个field在结构体(struct)中的偏移量******\n";
 81     #define FPOS(type, field) ((int)&(((type*)0)->field))
 82     struct SizeOfStruct
 83     {
 84         unsigned int s1;
 85         char s2;
 86         unsigned int s3;
 87         unsigned int s4;
 88     };
 89     cout << "SizeOfStruct: " << sizeof(SizeOfStruct) << endl;
 90     cout << "s1: " << FPOS(SizeOfStruct, s1) << endl;
 91     cout << "s2: " << FPOS(SizeOfStruct, s2) << endl;
 92     cout << "s3: " << FPOS(SizeOfStruct, s3) << endl;
 93     cout << "s4: " << FPOS(SizeOfStruct, s4) << endl;
 94 
 95     cout << "\n******得到一个field在结构体(struct)中的所占的偏移量******\n";
 96     #define FSIZE(type, field) sizeof(((type*)0)->field)
 97     cout << "SizeOfStruct: " << sizeof(SizeOfStruct) << endl;
 98     cout << "s1: " << FSIZE(SizeOfStruct, s1) << endl;
 99     cout << "s2: " << FSIZE(SizeOfStruct, s2) << endl;
100     cout << "s3: " << FSIZE(SizeOfStruct, s3) << endl;
101     cout << "s4: " << FSIZE(SizeOfStruct, s4) << endl;
102 
103     cout << "\n******按照LSB格式把两个直接转化为一个Word******\n";
104     #define MERGE_BYTE(byte1, byte2) (((word)((byte1)*256))+ (byte2))
105     byte mergeA = 0x10;
106     byte mergeB = 0x20;
107     cout << hex <<MERGE_BYTE(mergeA, mergeB) << endl;
108 
109 
110     cout << "\n******按照LSB格式把WORD拆分为byte******\n";
111     #define SEGMENTATION(com, byte1, byte2) {\
112         byte1 = ((com)/256);\
113         byte2 = ((com)&0xFF);\
114     }
115     word com = 0x1020;
116     byte byte1, byte2;
117     SEGMENTATION(com, byte1, byte2);
118     cout << hex << (int)byte1 << "\t" << (int)byte2 << endl;
119 
120     cout << "\n******得到一个字的高位和低位字节******\n";
121     #define WORD_LO(value) ((byte)((value)&0xFF))
122     #define WORD_HI(value) ((byte)((value)>>8))
123     word value = 0x1020;
124     byte low, high;
125     cout << hex << "value: " << value << endl
126          << "low: " << (int)WORD_LO(value) << endl
127          << "high: " << (int)WORD_HI(value) << endl;
128 
129     cout << "\n******返回一个比x大的8的倍数******\n";
130     #define SEED8(x) ( ((x)+7)/8*8 )
131     int seed1 = 11;
132     cout << "x = 10: " << SEED8(seed1) << endl;
133 
134     cout << "\n******将字母转换为大写******\n";
135     #define UPCASE(c) (((c) >= 0x61 && (c) <= 0x7A) ? ((c) - 0x20) : (c))
136     cout << "a: " << (char)UPCASE('a') << endl;
137 
138     cout << "\n******变量自增防止溢出******\n";
139     #define INC_VAL(val) ((val) = (((val)+1) > (val)) ? (val)+1 : (val))
140     unsigned int valueInc = 0xFFFFFFFF;
141     cout << "valueInc: " << valueInc << endl;
142     cout << "INC_VAL(valueInc): " << INC_VAL(valueInc) << endl;
143 
144     cout << "\n******返回数组元素的个数******\n";
145     #define ARR_SIZE(a) (sizeof((a))/sizeof((a)[0]))
146     int ARR[20] = {0};
147     cout << sizeof(ARR) << "\t" << sizeof(ARR[0]) << endl;
148     cout << "ARR[20]: " << ARR_SIZE(ARR) << endl;
149 
150     cout << "\n******调试跟踪宏******\n";
151     cout << "__LINE__: " << __LINE__ << endl
152          << "__FILE__: " << __FILE__ << endl
153          << "__DATE__: " << __DATE__ << endl
154          << "__TIME__: " << __TIME__ << endl
155          << "__STDC__: " << __STDC__ << endl;
156     #ifdef _DEBUG
157         cout << "hello: " << endl;
158     #endif // __DEBUG
159     int a12 = 10;
160     a12 ++;
161 
162     cout << "\n******宏定义防止错误使用大括号包含******\n";
163     #define BRACES_ERROR {cout << "BRACES ERROR!\n";}
164     #define DOWHILE_RIGHT do{\
165         cout << "DOWHILE RIGHT!\n";\
166     }while(0)
167 /*    if(1) //去掉注释会出现错误
168         BRACES_ERROR;
169     else
170     {
171     }
172 */
173     if(1)
174         DOWHILE_RIGHT;
175     else
176     {
177     }
178 
179     return  0;
180 }
181 
182 /**
183  *  实例总结:
184  *      特殊的define用法:
185  *          通过使用其特殊的用法,可以控制代码,这是非常有用的,
186  *          需要有一个意识,代码控制的是逻辑,所以宏定义是对代码进行的编程。
187  *      define的多行定义:
188  *          通过这种形式可以实现复杂的函数,不过宏定义的函数也有自己的弊端,在此从略。
189  *      条件编译:
190  *          条件编译的用处很多!其实,我们现在应该是更进一步的体会条件编译,
191  *          编译器的控制。
192  *      得到指定地址上的一个字节或者字:
193  *          这是一种很简单的用法,就是将地址转换为指定的格式(这里可以理解格式的本质)
194  *          之后,在对其进行取值!
195  *      调试跟踪宏:
196  *          __STDC__ 宏指令的意义是编译时定义的。一般来讲,如果__STDC__已经定义,
197  *          编译器将仅接受不包含任何非标准扩展的标准C/C++代码。如果实现是标准的,
198  *          则宏__STDC__含有十进制常量1。如果它含有任何其它数,则实现是非标准的。
199  *      宏定义防止错误使用大括号包含:
200  *          这是由于if语句固有的特性决定的if{};那么if语句结束,如果后面有else,也不会匹配!
201  *
202  */

 

转载于:https://www.cnblogs.com/rainyBlueSky/p/3265053.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值