如何使用宏定義求兩個數的最大值

源代碼如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define max_t(type, x, y) ({            \
    type __max1 = (x);          \
    type __max2 = (y);          \
    __max1 > __max2 ? __max1: __max2; })

int main(int argc, char* argv[])
{
    float a = max_t(float, -13.12, -14.21);
    float b = max_t(float, 13.12, 14.21);

    int c = max_t(int, -13, -14);
    int d = max_t(int, 13, 14);

    printf("a=%f\n", a);
    printf("b=%f\n", b);
    printf("c=%d\n", c);
    printf("d=%d\n", d);

    return 0;
}


運行結果如下所示:

a=-13.120000
b=14.210000
c=-13
d=14

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章