预处理指令

2016-07-29  本文已影响9人  coder_hong

预处理指令的概念

预处理的主要包含:

宏定义

 比如  #define ABC 10
右边的字符串也可以省略,比如  #define ABC
它的作用是在编译预处理时,将源程序中所有"宏名"替换成右边的"字符串",常用来定义常量。
#define 宏名(参数列表) 字符串

如:  #define average((a), (b)) ((a)+(b))/2

与函数的却别

条件编译

概念

一、基本用法

// 注意这里不可以判断变量 因为变量是在程序运行时定义的 可以对定义的宏进行判断
 #if 条件1
  ...code1...
 #elif 条件2
  ...code2...
 #else
  ...code3...
 #endif

// 使用实例
#define MAX 11

 #if MAX == 0
    printf("MAX是0");
 #elif MAX > 0
   printf("MAX大于0");
 #else    
   printf("MAX小于0");
#endif
#if defined(MAX)
     ...code...
 #endif
 #if !defined(MAX)
     ...code...
 #endif
如果前面已经定义过MAX这个宏,就将code编译进去。
#ifdef MAX
     ...code...
 #endif

- (2) #ifndef又和#if !defined()的用法基本一致
 #ifndef MAX
     ...code...
 #endif
上一篇 下一篇

猜你喜欢

热点阅读