Recently a product debugging found a problem, together to check a small partner's C program.
The problem is not big, but it exposes a little problem. For example: portb=0x23; He means to place the 5th and 2nd digits 1, but can you see the 5th and 2nd position 1?
In fact, it should be 0x24, he did not find this error, calculated wrong, so the correct is: portb=0x23;
In fact, we can completely avoid this mistake. Open our compiler's own header file, such as the AVR TINY13 header file has the following statement: # define PB5 5 # define PB4 4 # define PB3 3 # define PB2 2 # define PB1 1 # define
PB0 0 seems to be of little use, but it is useful. Portb=0x24 can be written as: portb= (1<<5) | (3<<0) In this way, it is not all of a sudden to see who and who.
It is also convenient for others to read.
Define's magical use of more than this, but the basic use of simple, to be flexible, to do.
The following is excerpt from the Internet define usage, I hope you can master and use. #* #define用法 * 1. The simple define definition #define MaxTime 10,001 simple maxtime is defined, it stands for 1000, if you write the IF in the program (I compiler replaces the maxtime before processing the code 1
000.
Such a definition would look similar to the normal constant definition const, but it is also different because the definition of define is more like a simple text replacement than as a volume, a problem that is particularly evident below. 2.define "function definition" define can accept some parameters like a function, as follows #define MAX (X,y) (x) > (y)?
(x):(y); This definition will return the larger of the two numbers, see. Because this "function" does not have type checking, it is like a function template, of course, it is absolutely not as safe as the template.
Can be used as a simple template.
But there are pitfalls in doing so, as follows: #define ADD (a,b) a+b;
It's not a problem in general use, but if there is a problem with the C * ADD (A,B) * d, the algebra is meant to be a+b and then multiplied by c,d, but because the define is used (it's just a simple substitution), the equation actually turns into c*a + b*d In addition one example: #define PIN (int*);
Pin a,b;
The intention is that A and B are both int-type pointers, but actually become int* a,b;
A is an int type pointer, and b is an int variable.
This should use a typedef instead of define so that A and B are all int pointers.
So when we define it, we develop a good habit of adding parentheses to all levels.
3. One-line definition of a macro #define a (x) t_# #x #define B (x) #@x #define C (x) #x We assume: x=1, then there are: a (1)------〉t_1 B (1)------' 1 ' C (1)------"1"
(here refer to Hustli's article) 4.define multiline definition define can replace more than one line of code, such as the macro definition in MFC (very classic, although disgusting) #define MACRO (arg1, arg2) do {//STMT1; STMT2;
//} while (0) the key is to add a "/" 5 for each line change. In the large-scale development process, especially in cross-platform and system software, define most important function is conditional compilation. This is: #ifdef WINDOWS ... #endif #ifdef LINUX ... #endif can set up the compilation environment by #define at compile time 6. How to define macros, cancel macros//define macros, and then do this #de Fine [macroname] [Macrovalue]//Cancel Macro #undef [macroname] Normal macro #define PI (3.1415926) macro #define with Parameters Max (A,b) ((a) > (b)?
(a), (b)) The key is that it is very easy to produce errors, including differences in machine and human understanding, and so on. 7. Conditional compilation #ifdef XXX ... (#else) ... #endif for example #ifdef dv22_aux_input #define AUX_MODE 3 #else #define Auy_mode 3 #endif #ifndef XXX ...
(#else) ... #endif 8. Header file (. h) can Beatles a file or a C file that contains duplicate inclusions (duplicate definitions) because the header file contains the possibility of nesting, then the C file may contain multiple occurrences of the same header file, which may result in a duplicate definition of the problem. Use conditional compilation switches to avoid duplicate inclusions (repeatFor example #ifndef __headerfilexxx__ #define __HEADERFILEXXX__ ... File contents ... #endif 9. #define中的 #, # # && #@ some time ago while looking at wince's code, I found it useful in macro definition to # #, as shown below #define GPEBLT_FUNCNAME (basename) SCODE (GPE::*) ( struct gpebltparms*) &gpe::# #basename in #define, the standard only defines # and # #两种操作.
#用来把参数转换成字符串, # #则用来连接两个前后两个参数, turn them into a string. 1#define ToString (a) #a 2ToString (a b Cd); A b cd 3ToString (a/n b CD); A 4//b CD 5ToString (a/n b CD);
A N B Cd 6 7 8#define ConCat (x, y) x # # Y 9ConCat ("ABC", "DEF");
10ConCat (123, 4);
11ConCat (123.0, 5);
12//================================= 13//Cat (123.0, 5.5);
14//Cat (' A ', ' B ');
15//Cat ("ABC", ' d ');
16//Cat (' a ', 1234);
17//Cat ("ABC", 1234); 18//===== above can ' t compile ======= 19 20 In addition, there is a use of online search: #@, convert the parameters to characters 1#define ToChar (a) #@a 2ToChar (a); A 3ToChar (AB); b 4ToChar (ABC); C 5ToChar (ABCD); D 6//tochar (ABCDE); Too many characters in constant 7ToChar (1.);
// . This is not seen on the standard. The above tests are done in the VS studio environment.
I don't know if GCC has the same usage.
Finally, attach the difference between the #define and the TypeDef 1 #define是预处理指令, in the compilation of preprocessing to make a simple replacement, do not check for correctness, does not matter whether the meaning is correct to bring in, only when compiling the source program has been expanded to find possible errors and error.
For example: #define PI 3.1415926 Program: Area=pi*r*r will be replaced with 3.1415926*r*r if you put the number 9 in #define statement into the letter G preprocessing also bring in. 2 The TypeDef is processed at compile time.
It gives an alias to an already existing type within its scope, but you cannot use the typedef specifier inside a functiondefinition.
3) typedef int * INT_PTR; With the #define INT_PTR int * Acts are represented int* by INT_PTR, but the two are different, as mentioned earlier, #define在预处理时进行简单的替换, and the typedef is not a simple replacement, but rather a type declared as a method of defining a variable.
Say Refer to (Xzgyb (Damour)) #define INT_PTR int * Int_ptr A, B; equivalent to int * A, B;
Just a simple macro replacement typedef int* INT_PTR; INT_PTR A, B;
A, b are pointers to int, typedef introduces a new mnemonic for int* This also explains why the following view is set up//qunkangli (the maintenance cost is proportional to the square of the programmer's Creativity) typedef int * PINT;
#define PINT int * Then: const pint p;//p cannot be changed, but the content that P points to can change const pint p;//p can change, but the content that P points to cannot be changed.
Pint is a pointer type const pint p is to lock the pointer to the P cannot be changed and the const pint P is the constint * p Lock is the object referred to by the pointer p.
4 should also be noted that #define is not a statement do not line at the end of the semicolon, or you will be replaced with a semicolon.