分类: C/C++
2008-05-11 23:48:29
#ifndef COMDEF_H
#define COMDEF_H
//头文件内容
#endif
#include
int main(int argc,char *argv[])
{
printf("lsdkfla\n");
}
#include
#include
int main(int argc,char *argv[])
{
printf("lsdkfla\n");
}
#gcc test1.c -Wall
#gcc test2.c -Wall
#gcc -Wall -static test1.c -o test1
#gcc -Wall -static test2.c -o test2
/*test.h*/
#include
int test(int a)
{
printf("test ...\n");
}
/*main.c*/
#include "test.h"
int main(int argc,char *argv[])
{
test(4);
}
#gcc -Wall main.c
#include
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’ 的上一个定义在此
#ifndef _TEST_H
#define _TEST_H
#include
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
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. */
#define MEM_B(x) (*((byte *)(x)))
#define MEM_W(x) (*((word *)(x)))
#include
#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
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
#include
#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
#define FPOS( type, field ) ( (dword) &((( type *) )0)-> field )
#include
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
“&”的运算级别小于“->”运算级别
#define FSIZ(type,field) sizeof(((type *) 0)->field )
#include
#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
#define FLIPW(ray) ((((word)(ray)[0])* 256)+(ray)[1])
#include
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
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
#define FLOPW( ray, val ) (ray)[0] = ((val) / 256); (ray)[1] = ((val) & 0xFF)
资料1:c语言的宏定义技巧
http://www.ednchina.com/blog/levension/21415/message.aspx
资料2:
http://gawaine.itpub.net/
资料3:
|
|
|
() [] -> . | 括号(函数等),数组,两种结构成员访问 |
|
! ~ ++ -- + -
* & (类型) sizeof |
否定,按位否定,增量,减量,正负号, 间接,取地址,类型转换,求大小 |
|
* / % | 乘,除,取模 |
|
+ - | 加,减 |
|
<< >> | 左移,右移 |
|
< <= >= > | 小于,小于等于,大于等于,大于 |
|
== != | 等于,不等于 |
|
& | 按位与 |
|
^ | 按位异或 |
|
| | 按位或 |
|
&& | 逻辑与 |
|
|| | 逻辑或 |
|
? : | 条件 |
|
= += -= *= /= &= ^= |= <<= >>= |
各种赋值 |
|
, | 逗号(顺序) |
|