#define ----typedef(关键字) 5个功能学习

来源:互联网 发布:windows 文件夹映射 编辑:程序博客网 时间:2024/05/24 01:47
  1. 宏:#define (关键字) (gcc -E test.c -o test.i)查看宏的替换
    #define SIZE 20 //宏的名称用大写 三段 用空格键分割
#define INT8    char#define INT16   short#define INT32   int#define UINT8   unsigned char#define LONG    (long long)

说明 :char short int float long double (long long)
32位 1 2 4 4 4 8 8 (4字节对齐)
64位 1 2 4 4 8 8 8 (8字节对齐)

tpyedef //关键字 ,取别名

typedef int  int_t;typedef long  long_t;

//这个结构体 有两个名字———–struct People——–people——
typedef struct
{
int age;
char sex;
}people;

2、头文件的定义(防止我们重复定义头文件)
#define _TEST_DEFINE_H
#define _TEST_DEFINE_H
//——————————–
extern int sum(int x,int y);
//———————————
#endif
3、运算
#define SUM(a,b) ( a+b )
#define MAX(a,b) ( a>b ? a:b )
#define MIN(a,b) ( a>b ? b:a )
#define SQURAE(x) ( x*x )
#define SQURAE(x) ( (x)*(x) )

4、代码注释
#define TEST_LOG
#ifdef TEST_LOG
void test_log(void)
{
//—————————–
}
#endif

#ifdef VOICES
void test_voices(void)
{
//————————-
}
#endif
void test_video(void)
{
//—————————
}

5、字符串连接 (# ##)

#define MY_STRACT(a,b) ( a#b )
#define ERR_LOG(fmt,args…) printf(fmt,##args)

//#define BUG
#ifdef BUG
#define ERR_LOG(fmt,args…) printf(fmt,args)
else
#define ERR_LOG(fmt,args…) {} //空操作,什么也不执行

6、地址操作
#define LED_PIN_A ( ( volatile unsigned shotr * )0x1002 )

0 0