C语言的宏定义和调试程序方法

 

1 防止一个头文件重复定义

格式如下

#ifndef COMDEF_H
#define COMDEF_H
//头文件内容
#endif

这个在很多的头文件的开头都有看到,就是弄不明白,什么叫重复定义???试个程序看看

  • 例题1 test1.c
#include <stdio.h>
int main(int argc,char *argv[])
{
printf("lsdkfla/n");
}
  • 例题2 test2.c
#include <stdio.h>
#include <stdio.h>
int main(int argc,char *argv[])
{
printf("lsdkfla/n");
}

现用动态文件的方法编辑

#gcc test1.c -Wall

编译结构,没有错误,肯定的。。。。

#gcc test2.c -Wall 

哦,也没有错误???那这个有什么用呢!!!不理解,是不是能在test2.c编译的结构中a.out中会有两个标准库stdio.h的库连接呢,我编译静态。 编译成静态文件:

#gcc -Wall -static test1.c -o test1

编译结果,没有错误!

#gcc -Wall -static test2.c -o test2

编译结构,也没有错误阿。。。 那用ls -l查看文件大小,哦,一样大。。。那个功能是什么作用呢,想了好久。。。突然想起来自己写一个头文件看看

  • 例题3 test.h main.c
/*test.h*/ 
#include <stdio.h>
int test(int a)
{
printf("test .../n");
}

/*main.c*/
#include "test.h"
int main(int argc,char *argv[])
{
test(4);
}

编译方法:

#gcc -Wall main.c

编译结果,没有错误。。。。 继续试验。。。。。

  • 例题4 test.h main.c

/*test.h*/

#include <stdio.h>
int test(int a)
{
printf("test .../n");
}

/*main.c*/
#include "test.h"
#include "test.h"
int main(int argc,char *argv[])
{
test(4);
}

编译结果: 终于出错了阿。。。呵呵。。。

test.h:3: 错误: ‘test’ 重定义
test.h:3: 错误: ‘test’ 的上一个定义在此

终于弄明白了。。。。。那么我加上宏定义看看。。。。 例题5 test.h main.c /*test.h*/

#ifndef _TEST_H
#define _TEST_H
#include <stdio.h>
int test(int a)
{
printf("test .../n");
}
#endif /*结束_TEST_H*/

/*main.c*/
#include "test.h"
#include "test.h"
int main(int argc,char *argv[])
{
test(4);
}

编译方法:

#gcc -Wall main.c

编译结果: 程序正常完成编译,没有出错信息。。。。

[ 编辑]

2 防止由于各种平台和编译器的不同,而产生的类型字节数

typedef  unsigned char      boolean;     /* Boolean value type. */
typedef unsigned long int uint32; /* Unsigned 32 bit value */
typedef unsigned short uint16; /* Unsigned 16 bit value */
typedef unsigned char uint8; /* Unsigned 8 bit value */
typedef signed long int int32; /* Signed 32 bit value */
typedef signed short int16; /* Signed 16 bit value */
typedef signed char int8; /* Signed 8 bit value */
//下面的不建议使用
typedef unsigned char byte; /* Unsigned 8 bit value type. */
typedef unsigned short word; /* Unsinged 16 bit value type. */
typedef unsigned long dword; /* Unsigned 32 bit value type. */
[ 编辑]

3 得到指定地址上的一个字节或字

取得指定地址的一个字

#define MEM_B(x) (*((byte *)(x)))

取得指定地址的一个字

#define MEM_W(x) (*((word *)(x)))

例题1 test.c

#include <stdio.h>
#define MEM_B(x) (*((byte *)(x)))
int main(int argc,char *argv[])
{
char a='c';
printf("&a==%c /n",MEM_B(&a));
}

编译方法:

#gcc -Wall test.c

执行结果:

&a==c
[ 编辑]

4 求最大值和最小值

定义方法:

#define  MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )

例题:test.c

#include <stdio.h>
#define MAX(x,y) (((x)>(y))?(x):(y))
int main(int argc,char *argv[])
{
printf("max==%d /n",MAX(4,5));
}

编译方式:

#gcc -Wall test.c

执行结果:

#max==5
[ 编辑]

5 得到一个field在结构体(struct)中的偏移量

定义方法:

#define FPOS( type, field ) ( (dword) &((( type *) )0)-> field )

这个是最郁闷的一个,终于搞定了。。。。郁闷的typedef和运算规则 例题 test.c

#include <stdio.h>
typedef unsigned long dword;
#define FPOS( type,field) (dword)&(((type *)0)->field)
typedef struct test
{
int a;
int b;
char c ;
}d;
int main(int argc,char *argv[])
{
printf("offset==%d /n",FPOS(d,b));
}

编译方法:

#gcc -Wall test.c

执行结果

#offset==0x4
“&”的运算级别小于“->”运算级别 
特别推荐C语言运算符表
</tr> </table>
[ 编辑]

6 得到一个结构体中field所占用的字节数

定义方法:

#define FSIZ(type,field) sizeof(((type *) 0)->field )

例题: test.c

#include <stdio.h>
#define FSIZ(type,field) sizeof(((type *) 0)->field)
struct test
{
int a;
int b;
char c;
};
int main(int argc,char *argv[])
{
printf("sizeof==%d /n",FSIZ(struct test,b);
}

编译方法:

#gcc -Wall test.c

执行结果:

#sizeof=4
[ 编辑]

7 按照LSB格式把两个字节转化为一个Word

提示: LSB和MSB的定义

  • LSB(Least Significant Bit)是“意义最小的字位”。
  • MSB(Most Significant Bit)是“意义最大的字位”

定义格式

#define  FLIPW(ray) ((((word)(ray)[0])* 256)+(ray)[1])

例题 test.c

#include <stdio.h>
typedef unsigned short word;
#define FLIPW(ray) ((((word)(ray)[0])* 256)+(ray)[1])
int main(int argc,char *argv[])
{
char test[2]={0x06,0x07};
printf("LSB==%d /n",FLIPW(test));
}

编译方法:

#gcc -Wall test.c

执行结构

#1541

???这个数是??? 6*256+5=1541 突然见有了个想法,可以不可以变成大字节的顺序,看一下。。。 例题:test.c

  1. include <stdio.h>
typedef unsigned short word;
#define FLIPW(ray) ((((word)(ray)[1])* 256)+(ray)[2])
int main(int argc,char *argv[])
{
char test[2]={0x06,0x07};
printf("LSB==%d /n",FLIPW(test));
}

编译方法:

#gcc -Wall test.c

执行结构

#1286

??这个数是??? 5*256+6=1286

[ 编辑]

8 按照LSB格式把一个Word转化为两个字节

定义方式:

#define  FLOPW( ray, val ) (ray)[0] = ((val) / 256); (ray)[1] = ((val) & 0xFF)
[ 编辑]

9 参考资料

资料1:c语言的宏定义技巧
http://www.ednchina.com/blog/levension/21415/message.aspx
资料2:
http://gawaine.itpub.net/
资料3:
    http://www.yesky.com/309/1853309.shtml

运算符
解释
结合方式
() [] -> .括号(函数等),数组,两种结构成员访问
由左向右
! ~ ++ -- + - 

* & (类型) sizeof</td>

否定,按位否定,增量,减量,正负号, <p>间接,取地址,类型转换,求大小
由右向左
* / %乘,除,取模
由左向右
+ -加,减
由左向右
<< >>左移,右移
由左向右
< <= >= >小于,小于等于,大于等于,大于
由左向右
== !=等于,不等于
由左向右
&按位与
由左向右
^按位异或
由左向右
|按位或
由左向右
&&逻辑与
由左向右
||逻辑或
由左向右
? :条件
由右向左
= += -= *= /=  <p>&= ^= |= <<= >>=各种赋值
由右向左
,逗号(顺序)
由左向右
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值