Chinaunix首页 | 论坛 | 博客
  • 博客访问: 288759
  • 博文数量: 134
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 118
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-01 14:02
文章分类

全部博文(134)

文章存档

2015年(2)

2014年(4)

2013年(128)

分类: C/C++

2013-08-01 14:14:36

原文地址:预处理 _0224 作者:丫叩酱


点击(此处)折叠或打开

  1. 1.#若宏的替换列表由多个表达式构成,则通过’,’进行多个表达式的连接

  2. #include <stdio.h>
  3. #define ECHO(str) (gets(str),puts(str))

  4. int main(int argc, const char *argv[])
  5. {
  6.     char str[32];
  7.     
  8.     ECHO(str);
  9.     return 0;
  10. }
  11. 2.#若宏的替换列表由多条语句构成,则使用do{}while(0)的结构.while(0)后无;
  12. #include <stdio.h>
  13. #define SETN(N) do\
  14. {\
  15.     if(N==0)\
  16.         printf("N=0\n");\
  17.     else\
  18.         printf("N!=0\n");\
  19. }while(0)

  20. int main(int argc, const char *argv[])
  21. {
  22.     int n=0;
  23.     SETN(n);
  24.     return 0;
  25. }
  26. 3.#在带参数的宏定义当中一定要毫不吝啬地加(),来人为的控制优先级和接合性
  27. #include <stdio.h>

  28. #define CUB(N) ((N)*(N)*(N))

  29. int main(int argc, const char *argv[])
  30. {
  31.     int x;
  32.     printf("x=");
  33.     scanf("%d",&x);
  34.     printf("x^3=%d\n",CUB(x));
  35.     return 0;
  36. }
  37. 4.#练习函数的宏定义
  38. #include <stdio.h>
  39. #define MOD(N) ((N)%(4))
  40. int main(int argc, const char *argv[])
  41. {
  42.     int n;
  43.     printf("n=");
  44.     scanf("%d",&n);
  45.     printf("MOD(N%4)=%d\n",MOD(n));
  46.     return 0;
  47. }
  48. 5.#通常#开头的指令只占一行,若指令较长可使用’\’进行续航,要求’\’后紧跟换行符
  49.   #函数的宏定义中,不能包含return语句
  50. #include <stdio.h>
  51. #define FUN(x,y) do\
  52. {\
  53.     if((x*y)<100)\
  54.         printf("1\n");\
  55.     else\
  56.         printf("0\n");\
  57. }while(0)

  58. int main(int argc, const char *argv[])
  59. {
  60.     int x,y,n;
  61.     printf("x y\n");
  62.     scanf("%d %d",&x,&y);
  63.     FUN(x,y);
  64.     return 0;
  65. }
  66. 6.#集合了函数及宏的有点,既进行参数检查又提高了运行速度
  67. #include <stdio.h>

  68. inline int max(int a,int b)
  69. {
  70.     return a>b?a:b;
  71. }

  72. int main(int argc, const char *argv[])
  73. {
  74.     int n;
  75.     n=max(3,7);
  76.     printf("n=%d\n",n);
  77.     return 0;
  78. }
  79. 7.#include <*.h> 默认路径搜索头文件/usr/include/
  80.   #include "*.h" 搜索用户指定的路径。一般不指定绝对路径。
  81.                     用gcc -I./include选项,搜索当前目录的include文件夹
  82. 大型项目的组织结构:
  83.        project下:
  84.                 src: *.c源文件
  85.                 include:包含的头文件
  86.                 lib:
  87.                 Makefile文件
  88. 8.#条件编译放在程序中间
  89.   1)#if 常量表达式
  90.      ···
  91.     #endif
  92.   2)#ifdef DEBUG #if defined(DEBUG1)
  93.      ··· #elif defined(DEBUG2)
  94.   #endif #else
  95.   3)#ifndef DEBUG ···
  96.     #define DEBUG #endif
  97.      ···
  98.     #endif


  99. #include <stdio.h>

  100. int main(int argc, const char *argv[])
  101. {
  102.     #if 0
  103.         printf("显示注释内容!\n");
  104.     #endif
  105.     return 0;
  106. }
  107. 9.#练习使用##ifdef 宏···#elif defined()···#endif
  108.     
  109. #include <stdio.h>

  110. int main(int argc, const char *argv[])
  111. {
  112.     #ifdef ENGLISH
  113.         printf("Insert Disk 1\n");
  114.     #elif defined(FRENCH)
  115.         printf("Inserzet Le Disque 1\n");
  116.     #elif defined(SPANISH)
  117.         printf("Inserte EI Disco 1\n");
  118.     #endif
  119.     
  120.     return 0;
  121. }
  122. 10.#通常只有测试时才使用assert
  123. #include <stdio.h>
  124. //#define NDEBUG
  125. #include <assert.h>

  126. int main(int argc, const char *argv[])
  127. {
  128.     char str[20]="hello";
  129.     int i;

  130.     assert(i>=0&&i<50);
  131.     while(str[i])
  132.         printf("%c",str[i]);
  133.     return 0;
  134. }
  135. 11.# #、进行字符串化,该运算符仅允许出现在带参数的替换列表中。如下:输出ab
  136. #include <stdio.h>

  137. #define STR(x) #x

  138. int main(int argc, const char *argv[])
  139. {
  140.     char *p;
  141.     p=STR(ab);
  142.     printf("%s\n",p);
  143.     return 0;
  144. }
  145. 13.# ##标记粘合运算符的使用
  146. #include <stdio.h>

  147. #define GENERIC_MAX(type) \
  148.     type type##_max(type a, type b) \
  149.     {     \
  150.         return (a > b) ? a : b;\
  151.     }

  152. GENERIC_MAX(int)
  153. GENERIC_MAX(double)
  154. int main(void)
  155. {
  156.     int max = int_max(2, 5);
  157.     double m = double_max(2.69, 5.98);

  158.     printf("max = %d\n", max);
  159.     printf("m = %f\n", m);
  160.     return 0;
  161. }

阅读(313) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~