有关宏的一些问题

今天在看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 40
void *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 40


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;
}
void *function1(void*arg)
{
#define MAX 50

	 return NULL;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值