宏定义define

(1)define

	  1 #include <stdio.h>
	  2 
	  3 #define S(a) #a  						//	使“a的内容”字符串化
	  4 #define NAME(a, b)  a##b  		//	拼接a和b成为一个字符串
	  5 #define MN hello   						//普通的简单替换
	  6 #define NM “hello”						//替换成字符串
	  7 
	  8 int main(int argc, const char *argv[])
	  9 {
	 10     int hello = 211;
	 11     int world = 985;
	 12     printf("s = %s\n", S(100));
	 13     printf("s = %d\n", NAME(worl, d));
	 14     printf("hello = %d\n", NAME(M, N));
	 15 
	 16     return 0;
	 17 }                                                                                                                                                                   
	~        

(2)define把没有实际含义的数字封装成一个有意义的相关的名字

		#define  LEDOFF   0
		#define  LEDON     1

(3) 效率性的替换(绑定一起的运算加上小括号())

		#define PI 3.1415926
		#define MN (985 + 211)

注释:

		#if 0
		
		语句块1;
		
		#else
		
		语句块2;
		
		#endif

(4)头文件防止重复,写法:

		#ifndef  __MATH__H__
		#define __MATH__H__
	
		函数声明;
	
		#endif
发布了13 篇原创文章 · 获赞 0 · 访问量 138

猜你喜欢

转载自blog.csdn.net/m0_46170433/article/details/104672619