Programmer --- C language details 9
Main Content: macro definition, max (a, B) macro definition details, size judgment, (int &) What does a mean
# If 1 # include
// Note the space # define F (x)-1) // F indicates the end # define F (x)-1) // F (x) indicates # define T1 struct type * T1 a, B; // The result is not obtained when multiple variables are defined above and is parsed as struct type *, b; a is the pointer to the struct, and B is defined as a struct. // The macro definition of the classic pen question is the maximum value. // # define max (a, B) a> B? A: B // This is easy to make a mistake. The following comparison is not easy to make a mistake, but there will also be errors # define max (a, B) (a)> (B )? (A) (B) // if the full score is not obtained, the problem may still occur. // At the same time, the macro definition does not consider inputting different types of parameters, /** if an operand is used in two places, it will be evaluated twice. * If the expression max (a, B) has side effects, the calculation is performed once in an hour, and the calculation is also performed once * the error example is as follows: */int main (int argc, char ** argv) {int x [3] = {2, 3, 1}; int biggest = x [0]; int I = 1; while (I <3) {biggest = max (biggest, x [I ++]); // is split into biggest> x [I ++]? Biggest: x [I ++], (biggest = 2) <(x [1] = 3) // The key point is that I has side effects when comparing the size, I ++ added 1, biggest = x [2]} when I was later. // the solution is to confirm that the parameter has no side effects, or directly write the max function printf ("biggest is % d \ n", biggest); // A maxmacro definition that considers the input of different variable types. // The correct answer should be: # define MAX (a, B) ({\ const typeof (a) _ a = a; \ const typeof (B) _ B = B; \ (void) (& _ a = & _ B); \ _ a> _ B? _ A: _ B;}) return 0;} // The following is the method for testing the size of the end. # if # elif needs to be enabled by yourself. // Add one (int &) what does a mean? (Int &) a is * (int *) (& a) # elif 1 # include
Int main () {int x = 1; char * p = (char *) & x; if (* p) {printf ("little \ n ");} else {printf ("large \ n") ;}return 0 ;}# else # include
Int main () {short int x; char x1, x2; x = 0x1122; x1 = (char *) & x) [0]; // low address x2 = (char *) & x) [1]; // high address printf ("x1 = % x \ n", x1 ); printf ("x2 = % x \ n", x2); return 0 ;}# endif
Output:
Macro definition and big small terminal