1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
// #ifndef常见用法,防止包含多个文件或多次包含同一文件后重复定义 #include <stdio.h> #ifndef _STDIO_H //通常用文件名、下划线定义宏 #define _STDIO_H // ... 其他定义 #endif int main(int argc, char const *argv[]) { return 0; } // #if #elif #if defined() #include <stdio.h> #define FLAG 3 #if FLAG == 1 #include <string.h> #elif FLAG == 2 // ... #elif FLAG == 3 // ... #else // ... #endif // 使用 if defined()代替 ifdef 语法的好处就是可以使用elif #if defined(FLAG) // ... #elif defined(VAX) // ... #elif defined(MAC) // ... #else // ... #endif int main(int argc, char const *argv[]) { return 0; } // 预定义宏 __DATE__ __FILE__ __LINE__ __STDC__ __STDC_HOSTED__ __STDC_VERSION__ __TIME__ // c99 __func__(预定义标识符,具有函数作用域) #include <stdio.h> void why_me(); int main(int argc, char const *argv[]) { printf("the file is %s\n", __FILE__); printf("the date is %s\n", __DATE__); printf("the time is %s\n", __TIME__); printf("this version is %ld\n", __STDC_HOSTED__); printf("this is line %d\n", __LINE__); printf("this fucntion is %s\n", __func__); why_me(); return 0; } void why_me() { printf("this is line %d\n", __LINE__); printf("this fucntion is %s\n", __func__); } // #line指令用于重置由__LINE__和__FILE__宏报告的行号和文件名 #include <stdio.h> #line 1000 //把当前行号重置为1000 #line 10 "cool.c" //把行号重置为10,文件名重置为cool.c int main(int argc, char const *argv[]) { return 0; } // 如果version不是C99中断编译 #include <stdio.h> #if __STDC_VERSION__ != 199901L #error Not C99 #endif int main(int argc, char const *argv[]) { return 0; } // 在现代编译器中,可用命令行参数或IDE菜单修改编译器的某些配置 // 也可用 #pragma 将编译器指令置于源代码中 #include <stdio.h> // 在开发C99时 用C9X代表C99 编译器可以使用pragma来启用对C9X的支持 #pragma c9x on // c99提供了_Pragma预处理器运算符 _Pragma("nonstandardtreatmenttypeB on") // 等价于 #pragma nonstandardtreatmenttypeB on // 如果没有定义LIMIT宏,编译时提示LIMIT not be defined #ifndef LIMIT #pragma message("LIMIT not be defined") #endif int main(int argc, char const *argv[]) { return 0; } |