#define 宏定义,gcc proprocessor 将会在宏出现的每一处扩展代码
#elif 用于 #if 结构
#else 用于 #if 结构
#error 输出信息并终止 proprocessor 处理
#endif 用法 #if 结构
#if 结构类似 if - else/else if - 结构,表达式 non-zero 为真,否则为假
#ifdef 表达式为真,则编译器编译 #ifdef - #endif 之间的代码。
#ifndef 表达式为假,则编译器编译 #ifndef - #endif 之间的代码。
#include 搜索目标路径直到找到文件,然后插入文件内容进入被include文件处
#line 设置行号,一般用于debug
#pragma 提供附加信息给编译器用于输出
#undef 解除宏定义
#warning 给 preprocessor 提供一个 warning 信息。
## 连接字串
■ #define MONCK(MSG) \
printf("The Term is " #MSG " is a string\n")
MONCK(A to B);
扩展后: printf("The Term is A to B is a string\n");
■ #define err(...) \
fprintf(stderr, __VA_ARGS__)
err("%s %d\n", "error:", 48);
扩展后: fprintf(stderr,"%s %d\n", "error:", 48);
■ #warning 和 #error
preprocessor 停止工作:
#ifndef __unix__
#error "only work on unix system"
#endif
preprocessor 直接打印信息:
#warning "warning: work on windows sytem"
■ #if #elif #else 和 #endif
#if COUNT
printf("COUNT is non-zeor\n");
#endif
---------------------------------
#if COUNT
printf("COUNT is non-zeor\n");
#else
printf("COUNT is zeor\n");
#endif
--------------------------------
#if I<=10
printf("I <= 10\n");
#elif I<=20
printf("I <= 20\n);
#else
printf("I>20\n");
#endif
■ #ifdef #ifndef #else 和 #endif
#ifdef MAX
int arrary[MAX];
#else
#define MAX 50
int array[MAX];
#endif
----------------------------------------
#ifndef MAX
#define MAX 50
#endif
int arrary[MAX];
■ #include
#include <stdio.h>
#include "myheader.h"
#include <> 将会查找标准系统目录。在 linux 下一般是:
/usr/local/include
/usr/include
/usr/lib/gcc/target/version/include
/usr/targer/include
#include "" 将会查找当前工作目录下。
gcc 命令行选项 -I 指示附加的 include 目录。
■ ## 连接符
#define PASTE(a) a##house
#define PASTE("mik")
preprocessor 扩展后: mikhouse
■ 命令行选项:
cpp -E -dM test.c 将会输出系统及 test.c 定义的宏
■ 命令
cpp test.c > test.i 输出 preprocessor 处理结果文件