读书笔记: 宏(macro)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>

#define IS_EMPTY (ptr) (!(ptr))
#define IS_FULL (ptr) (!(ptr))
//IS_EMPTY 判断传入链表是否为空
//IS_FULL 看malloc的temp地址区域是否为空,空则说明内存满

#define SWAP(x,y,t) ((t) = (x),(x)=(y),(y)=(t))

void swap(int *x,int *y){
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

#define COMPARE(x,y) ( ((x) < (y)) ? -1 : ((x) == (y)) ? 0 : 1)

int compare(int x,int y){
    if (x < y) return -1;
    else if (x == y) return 0;
        else return 1;
}

int main (int argc, const char * argv[])
{
    int a,b;

    a= 5;
    b= 10;
    printf("%d %d \n",compare(a, b),COMPARE(a,b));

    a= 10;
    b= 10;
    printf("%d %d \n",compare(a, b),COMPARE(a,b));

    a= 50;
    b= 10;
    printf("%d %d \n",compare(a, b),COMPARE(a,b));

    printf("%d %d \n",a,b);
    swap(&a,&b);
    printf("%d %d \n",a,b);

    int temp;
    SWAP(a, b, temp);
    printf("%d %d \n",a,b);

    return 0;
}