您的位置:首页 > 编程语言 > C语言/C++

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

2007-04-23 19:16 567 查看

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

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[/code] 
运算符解释结合方式
() [] -> .括号(函数等),数组,两种结构成员访问由左向右
! ~ ++ -- + -
* & (类型) sizeof</td>

否定,按位否定,增量,减量,正负号, <p>间接,取地址,类型转换,求大小由右向左
* / %乘,除,取模由左向右
+ -加,减由左向右
<< >>左移,右移由左向右
< <= >= >小于,小于等于,大于等于,大于由左向右
== !=等于,不等于由左向右
&按位与由左向右
^按位异或由左向右
|按位或由左向右
&&逻辑与由左向右
||逻辑或由左向右
? :条件由右向左
= += -= *= /= <p>&= ^= |= <<= >>=各种赋值由右向左
,逗号(顺序)由左向右
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: