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>&= ^= |= <<= >>= | 各种赋值 | 由右向左 |
, | 逗号(顺序) | 由左向右 |
相关文章推荐
- c语言 调试程序必备 c标准 宏 调试方法
- Linux 程序开发打印 Debug 信息的使用技巧--C语言中几种输出调试信息的方法
- Linux 程序开发打印 Debug 信息的使用技巧--C语言中几种输出调试信息的方法
- 在html里调试CGI程序的一个方法-针对C语言
- android native 程序崩溃的调试方法
- Linux下C++程序调试及除错方法
- C语言常用宏定义 __FILE__,__LINE__,FUNCTION__实现代码跟踪调试(linux下c语言编程)
- C语言中system()执行cmd命令打开关闭程序的方法
- xcode免证书真机调试iphone(ipad)程序的具体方法(总结贴)转贴)
- Windbg 内核态调试用户态程序然后下断点正确触发方法(亲自实现发现有效)
- c语言中字符串操作常见错误及调试方法
- C应用程序增加调试便利的方法,程序运行过程中输出运行的源文件名称 函数名称和当前行号
- 用python + hadoop streaming 编写分布式程序的本地调试方法
- WinCE程序调试方法
- 关于Android系统调试程序经常出现的错误解决方法(一)
- 解决Eclipse无法调试J2ME程序的配置方法
- 使用VS2010工具编写C程序运行方法及程序调试问题
- Eclipse调试程序的方法
- 使用C语言获取程序的执行时间的方法-clock函数
- android ndk程序调试方法