有关宏的一些问题

来源:互联网 发布:唇语软件下载 编辑:程序博客网 时间:2024/06/05 03:19

今天在看linux 中tcphdr的数据结构的时候,发现struct里面可以使用#define,进而想到那些地方可以使用#define,其是否有作用域的问题?

#include<stdio.h>struct tcphdr{unsigned int param1;unsigned int parametor1:1;unsigned int parametor2:7;unsigned int parametor3:24;#define MAX 30};//#define MAX 40void *function1(void*arg){#define MAX 50 return NULL;}//void *function1(void *arg);int main(){ tcphdr m;function1((void*)&m);m.param1 = 20;m.parametor1 =1;m.parametor2 =40;m.parametor3 = 23243;int a = MAX;printf("%d\n", a);printf("%d, %d, %d \n", m.parametor1, m.parametor2, m.parametor3);return 0;}

其中a的输出为50是与之最近的MAX

将程序修改一下

#include<stdio.h>struct tcphdr{unsigned int param1;unsigned int parametor1:1;unsigned int parametor2:7;unsigned int parametor3:24;#define MAX 30};//#define MAX 40void *function1(void *arg);int main(){ tcphdr m;function1((void*)&m);m.param1 = 20;m.parametor1 =1;m.parametor2 =40;m.parametor3 = 23243;int a = MAX;printf("%d\n", a);printf("%d, %d, %d \n", m.parametor1, m.parametor2, m.parametor3);return 0;}void *function1(void*arg){#define MAX 50 return NULL;}

a的输出结果为30,是与之最近(在其前面的#define MAX 30),这样是否可以说明编译器在编译过程中是按由上而下进行处理,然后在替换的过程中,选择其最新的宏定义来替换?仍待发现...

原创粉丝点击