c语言学习笔记十八

来源:互联网 发布:sx2150k基本数据 编辑:程序博客网 时间:2024/04/30 14:54
预处理


预处理的步骤:
 
 1 把三连符替换成相应的单字符
   替换规则:
     ??=       #
     ??/       \
     ??’      ^
     ??(       [
     ??)       ]
     ??!       |
     ??<       {
     ??>       }
     ??-       ~
 2 把\字符续行的多行代码接成一行
 3 把注释替换成一个空格
 4 预处理器把剩下的逻辑代码划分成预处理token和空白字符,
 5 在token中识别出预处理指示,作相应动作
   #inclucd  引入相应的源文件
   宏定义作宏展开
 6 替换字符常量和字符串中的转义字符
 7 把代码中相邻的字符串连接起来
 8 去掉空白字符(空格/换行/水平tab/垂直tab)  
 




宏定义




变量宏定义:
  示例代码:
   #define N 10
   #define STR "hello world!"


函数式宏定义
 示例代码:
   #define MAX(a,b) ((a)>b?(a):(b))
   k=MAX(i&0x0f,j&0x0f)
   
  展开宏定义:cpp test25.c或是gcc -E


 与真正函数的区别:
  1 参数没有类型,不作参数类型检查,只是形式上的替换
  2 生成指令不同
  3 内层括号不能省
  4 调用函数时先求实参表达式的值再传给形参
 




内联函数 (inline)
 调用时尽可能的快,可以当普通函数调用实现,也可以用宏展开
  示例代码:
  static inline void down_read(struct rw_semaphore *sem){
  might_sleep();
        rwsemtrace(sem,"Entering down_read");
_down_read(sem);
rwsemtrace(sem,"Leaving down_read")
  }


示例代码如下:


 inline int MAX(int a,int b){
return a>b?a:b;
}
int a[]={9,3,5,2,1,0,8,7,6,4};
int max(int n){
return n==0?a[0]:MAX(a[n],max(n-1));
}
int main(void){
max(9);
return 0;
}


编译代码:
 gcc test25.c -g
 
反汇编:(MAX是作为普通函数调用的)
 objdump -dS a.out


inline int MAX(int a,int b){
 80483b4: 55                   push   %ebp
 80483b5: 89 e5                mov    %esp,%ebp
return a>b?a:b;
 80483b7: 8b 45 08             mov    0x8(%ebp),%eax
 80483ba: 39 45 0c             cmp    %eax,0xc(%ebp)
 80483bd: 0f 4d 45 0c          cmovge 0xc(%ebp),%eax
}
 80483c1: 5d                   pop    %ebp
 80483c2: c3                   ret    


080483c3 <max>:
int a[]={9,3,5,2,1,0,8,7,6,4};
int max(int n){
 80483c3: 55                   push   %ebp
 80483c4: 89 e5                mov    %esp,%ebp
 80483c6: 83 ec 18             sub    $0x18,%esp
return n==0?a[0]:MAX(a[n],max(n-1));
 80483c9: 83 7d 08 00          cmpl   $0x0,0x8(%ebp)
 80483cd: 75 07                jne    80483d6 <max+0x13>
 80483cf: a1 40 a0 04 08       mov    0x804a040,%eax
 80483d4: eb 24                jmp    80483fa <max+0x37>
 80483d6: 8b 45 08             mov    0x8(%ebp),%eax
 80483d9: 83 e8 01             sub    $0x1,%eax
 80483dc: 89 04 24             mov    %eax,(%esp)
 80483df: e8 df ff ff ff       call   80483c3 <max>
 80483e4: 8b 55 08             mov    0x8(%ebp),%edx
 80483e7: 8b 14 95 40 a0 04 08 mov    0x804a040(,%edx,4),%edx
 80483ee: 89 44 24 04          mov    %eax,0x4(%esp)
 80483f2: 89 14 24             mov    %edx,(%esp)
 80483f5: e8 ba ff ff ff       call   80483b4 <MAX>
}
 80483fa: c9                   leave  
 80483fb: c3                   ret    


080483fc <main>:
int main(void){
 80483fc: 55                   push   %ebp
 80483fd: 89 e5                mov    %esp,%ebp
 80483ff: 83 e4 f0             and    $0xfffffff0,%esp
 8048402: 83 ec 10             sub    $0x10,%esp
max(9);
 8048405: c7 04 24 09 00 00 00 movl   $0x9,(%esp)
 804840c: e8 b2 ff ff ff       call   80483c3 <max>
return 0;
 8048411: b8 00 00 00 00       mov    $0x0,%eax
}
 8048416: c9                   leave  
 8048417: c3                   ret    
 8048418: 90                   nop
 8048419: 90                   nop
 804841a: 90                   nop
 804841b: 90                   nop
 804841c: 90                   nop
 804841d: 90                   nop
 804841e: 90                   nop
 804841f: 90                   nop


指定优代选项编译
gcc test25.c -g -O


反汇编:
objdump -dS a.out
可以看到MAX函数的指令被内联在max函数中


#include<stdio.h>
inline int MAX(int a,int b){
 8048404: 55                   push   %ebp
 8048405: 89 e5                mov    %esp,%ebp
 8048407: 8b 45 08             mov    0x8(%ebp),%eax
 804840a: 8b 55 0c             mov    0xc(%ebp),%edx
 804840d: 39 c2                cmp    %eax,%edx
 804840f: 0f 4d c2             cmovge %edx,%eax
return a>b?a:b;
}
 8048412: 5d                   pop    %ebp
 8048413: c3                   ret    


08048414 <max>:
int a[]={3,5,2,1,9,8,7,6,4,10};
int max(int n){
 8048414: 55                   push   %ebp
 8048415: 89 e5                mov    %esp,%ebp
 8048417: 53                   push   %ebx
 8048418: 83 ec 14             sub    $0x14,%esp
 804841b: 8b 5d 08             mov    0x8(%ebp),%ebx
return  n==0?a[0]:MAX(a[n],max(n-1));
 804841e: 85 db                test   %ebx,%ebx
 8048420: 75 07                jne    8048429 <max+0x15>
 8048422: a1 40 a0 04 08       mov    0x804a040,%eax
 8048427: eb 17                jmp    8048440 <max+0x2c>
 8048429: 8d 43 ff             lea    -0x1(%ebx),%eax
 804842c: 89 04 24             mov    %eax,(%esp)
 804842f: e8 e0 ff ff ff       call   8048414 <max>
###################################################################
#include<stdio.h>
inline int MAX(int a,int b){
return a>b?a:b;
 8048434: 8b 14 9d 40 a0 04 08 mov    0x804a040(,%ebx,4),%edx
 804843b: 39 d0                cmp    %edx,%eax
 804843d: 0f 4c c2             cmovl  %edx,%eax
####################################################################
}
int a[]={3,5,2,1,9,8,7,6,4,10};
int max(int n){
return  n==0?a[0]:MAX(a[n],max(n-1));
}
 8048440: 83 c4 14             add    $0x14,%esp
 8048443: 5b                   pop    %ebx
 8048444: 5d                   pop    %ebp
 8048445: c3                   ret    


08048446 <main>:
int main(void){
 8048446: 55                   push   %ebp
 8048447: 89 e5                mov    %esp,%ebp
 8048449: 83 e4 f0             and    $0xfffffff0,%esp
 804844c: 83 ec 10             sub    $0x10,%esp
     //查找集合中前n位的最大值
        int temp=0;
temp=max(4);
 804844f: c7 04 24 04 00 00 00 movl   $0x4,(%esp)
 8048456: e8 b9 ff ff ff       ca


 


#,##运算符和可变参数


#用于创建字符串,#后面跟有人形参
示例代码如下:
 #define STR(s)  # s
 STR(hello    world)
 处理后:"hello word"
 用""括起来,多个空格变一个空格
 如果形参中包含字符常量或是字符串,展开后"变\"  \变\\




## 把前后两个预处理连接成一个预处理token,(函数式宏定义和变量宏定义都可以)
 示例代码:
 #define CONCAT(a,b) a##b
 CONCAT(con,cat);
  处理后为:concat


...可变参数,  __VA_ARGS__代表可变参数部分


示例代码如下:
#define showlist(...) printf(#__VA_ARGS__)
#define report(test, ...) (test)?printf(#test):printf(__VA_ARGS__)
showlist(the first,second,and third items);
report(x>y,"x is %d but y is %d ",x,y);


处理后:
printf("the first,second,third items");
((x>y)?printf("x>y"):printf("x is %d but y is %d ",x,y));
 


宏展开的部骤:


示例代码:
#define sh(x) printf("n #x"=%d,or %d\n",n##x,alt[x])
#define sub_z 26
sh(sub_z)


过程如下:
1 #x要替换成"sub_z"
2 n##x 换成nsub_z
3 把sub_z 换成26后放到alt[x]的位置
4 结果: printf("n" "sub_z" "=%d,or %d\n",nsub_z,alt[26])