#define macro definition class A summary of the entire interview questions - pretreatment, const, static and sizeof (a)

table of Contents

 

#define macro definitions to use

Experience macro definition

Macro definition of ambiguity ambiguity

How to use macros to define the connection parameters

 # Ifdef, # else, # endif to use in the program


#define macro definitions to use

Nature macro is simple text replacement.

As represented by a macro substitution of x and y maximum and minimum values ​​defined comparison:

#define MAX(x,y) (((x)>(y))?(x):(y));
#define MIN(x,y) (((x)<(y))?(x):(y));

When used, is also very simple, direct call on the line:

#include <iostream>
#include <stdio.h>

#define MAX(x,y) (((x)>(y))?(x):(y));
#define MIN(x,y) (((x)<(y))?(x):(y));

int main()
{
	int x = 2, y = 8;
	printf("x = %d, y = %d\n", x, y);

	int MAX_result = MAX(x, y);
	printf("MAX_result:%d\n",MAX_result);

	int MIN_result = MAX(x, y);
	printf("MIN_result:%d\n", MIN_result);

	system("pause");
	return 0;
}

 operation result:

Experience macro definition

  1. Behind macro definition statement, not a comma.
  2. To macro parameters need to be carefully enclosed in parentheses, simply because the macro text replacement, if not pay attention, can easily lead to ambiguities.
  3. Why use the above ternary operator (? :) it? This is because the operator can be more play than if-else optimized code, and more concise and clear writing!
  4. Do pay attention, just text replacement macro, not a function expression, what does that mean? Give you an example:
int main()
{
	int x = 2, y = 8;
	printf("x = %d, y = %d\n", x, y);

	//int MAX_result = MAX(x, y); 
	printf("MAX_result:%d\n", MAX(x, y)); //如果把中间变量MAX_result给去掉,直接把宏放在printf里面,是会报错的!

	system("pause");
	return 0;
}

Or the above code, but modified his position, on the error, it is only because the macro text replacement, if the "Max (x, y)" put the printf inside, an error occurs. And before the thought, if a function on the inside, it will not appear, because the function returns a result of the default, and let the results continue to run, but the macro does not. 

Macro definition of ambiguity ambiguity

For example, I use the following macro definitions want to achieve a square operation, if you write a macro SQR1 accordance with the normal way of thinking, then the results would not I expect the square of the desired result!

#include <iostream>

#define SQR1(x) (x*x)
#define SQR2(x) ((x)*(x))

int main()
{
	int a = 2, b = 4;

	int result1 = SQR1(a+b); // 执行的是2+4*2+4 = 14
	printf("SQR1执行平方操作的结果:%d\n", result1);

	int result2 = SQR2(a+b); // 执行的是 (2+4)*(2+4) = 36
	printf("SQR2执行平方操作的结果:%d\n", result2);

	system("pause");
	return 0;
}

operation result: 

Again, in the definition of the macro, be sure to consider the nature of the macro is just a simple text replacement, it is not a function, so that the bracketed place, we must add, there would be ambiguous ambiguity.

How to use macros to define the connection parameters

Well, since a simple text replacement macro only, so if you can use a macro reference numerals and parameters to connect to it?

In fact, it is to use the "##" sign

#include <iostream>

#define STR(s) #s
#define CONS(a,b) (int)(a##e##b)

int main()
{
	printf("%s",STR(vck)); //这样写可以
	printf("\n");

	printf(STR(vck)); //这样写也可以
	printf("\n");

	printf("%d", CONS(1, 2)); //1e2 ->10^2->100(1乘以10的2次方)
	printf("\n");

	system("pause");
	return 0;
}

 operation result:

 # Ifdef, # else, # endif to use in the program

Print messages between "#ifdef ... #endif" output will not come out.

Example, the following code logic, called DEBUG define a macro definition, then, the following (#ifdef DEBUG) If there is a macro definition of named DEBUG, the statement is executed between #endif #ifdef and, if not, jump over.

#include <stdio.h>
#include <stdlib.h>

#define DEBUG

int main()
{
	int i = 0;
	char c;
	while (1)
	{
		i++;
		c = getchar();
		if (c != '\n')
		{
			getchar();
		}
		if (c == 'q' || c == 'Q')
		{
#ifdef DEBUG
			printf("we got:%c, about to exit.\n", c);
#endif
			break;
		}
		else
		{
			printf("i = %d", i);
#ifdef DEBUG
			printf(", we got:%c", c);
#endif // DEBUG
			printf("\n");

		}
	}
	system("pause");
	return 0;
}

Published 271 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_17846375/article/details/104902578