c语言宏定义实现多个数的排序

4 篇文章 0 订阅

基础概念

1.三目运算符: ?:

int max;
max = 6 > 5 ? 3:2;

这条代码的意思是 6是否大于5,如果是,则结果为真,max = 3,如果不是,则结果为假,max = 2。

2 获取变量的类型: typeof

char a;
int size;
typeof (a) _a = a;//等价于 char _a = a;
size = sizeof(typeof(_a));//等价于 size = sizeof(char);

typeof (a) _a = a 等价于 char _a = a。
size = sizeof(typeof(_a));
这条代码的意思是用typeof 获取 _a的存储类型char, size = sizeof(char),size为1(char 占一个字节)。

宏定义实现多个整形数的最大值

1.宏定义实现2个整形数的最大值

#define MAX_2(a,b) ((a) >= (b) ? (a):(b))

2.宏定义实现3个整形数的最大值

#define MAX_3(a,b,c) ((((a) >= (b) ? (a):(b))) >= (c) ? (((a) >= (b) ? (a):(b))):(c))

3.宏定义实现4个整形数的最大值

#define MAX_4(a,b,c,d) ((((((a) >= (b) ? (a):(b))) >= (c) ? (((a) >= (b) ? \
(a):(b))):(c))) >= (d)  ? (((((a) >= (b) ? (a):(b))) >= (c) ? (((a) >= (b) ? \
(a):(b))):(c))):(d))

4.运算结果

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_2(a,b) ((a) >= (b) ? (a):(b))

#define MAX_3(a,b,c) ((((a) >= (b) ? (a):(b))) >= (c) ? (((a) >= (b) ? (a):(b))):(c))

#define MAX_4(a,b,c,d) ((((((a) >= (b) ? (a):(b))) >= (c) ? (((a) >= (b) ? \
(a):(b))):(c))) >= (d) ? (((((a) >= (b) ? (a):(b))) >= (c) ? (((a) >= (b) ? \
(a):(b))):(c))):(d))

void main(void)
{
    printf("max_2:%d\n",MAX_2(1,99));
    printf("max_3:%d\n",MAX_3(1111,99,188));
    printf("max_4:%d\n",MAX_4(11,199,188,1));
}
输出结果
liu@ubuntu:~/code_test/c_test$ ./a.out 
max_2:99
max_3:1111
max_4:199
liu@ubuntu:~/code_test/c_test$ 

5.替换法快速实现多个数值的最大值

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_2(a,b) ((a) >= (b) ? (a):(b))
#define MAX_3(a,b,c) ((MAX_2(a,b)) >= (c) ? (MAX_2(a,b)):(c))
#define MAX_4(a,b,c,d) ((MAX_3(a,b,c)) >= (d) ? (MAX_3(a,b,c)):(d))
#define MAX_5(a,b,c,d,e) ((MAX_4(a,b,c,d)) >= (e) ? (MAX_4(a,b,c,d)):(e))

// #define MAX_3(a,b,c) ((((a) >= (b) ? (a):(b))) >= (c) ? (((a) >= (b) ? (a):(b))):(c))


// #define MAX_4(a,b,c,d) ((((((a) >= (b) ? (a):(b))) >= (c) ? (((a) >= (b) ? \
// (a):(b))):(c))) >= (d) ? (((((a) >= (b) ? (a):(b))) >= (c) ? (((a) >= (b) ? \
// (a):(b))):(c))):(d))

void main(void)
{
    printf("max_2:%d\n",MAX_2(1,99));
    printf("max_3:%d\n",MAX_3(1111,99,188));
    printf("max_4:%d\n",MAX_4(11,199,188,1));
    printf("max_5:%d\n",MAX_5(11,199,2000,188,15555));
}
输出结果
liu@ubuntu:~/code_test/c_test$ ./a.out 
max_2:99
max_3:1111
max_4:199
max_5:15555
liu@ubuntu:~/code_test/c_test$ 

6.多个任意类型数值的最大值或最小值

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_2(a,b) ({\
                        typeof(a) _a = a;\
                        typeof(b) _b = b;\
                        (_a) >= (_b) ? (_a):(_b);\
                    })

#define MAX_3(a,b,c) ({typeof(c) _c = c;(MAX_2(a,b)) >= (_c) ? (MAX_2(a,b)):(_c);})
#define MAX_4(a,b,c,d) ({typeof(d) _d = d;(MAX_3(a,b,c)) >= (_d) ? (MAX_3(a,b,c)):(_d);})
#define MAX_5(a,b,c,d,e) ({ typeof(e) _e = e;(MAX_4(a,b,c,d)) >= (_e) ? (MAX_4(a,b,c,d)):(_e);})

void main(void)
{
    char a;
    int b;
    int a_size;
    int b_size;

    a_size = sizeof(typeof(a));

    typeof (b) _a = b;
    b_size = sizeof(typeof(_a));
       
    printf("a_size:%d\n",a_size);
    printf("b_size:%d\n",b_size);

    printf("max_2:%d\n",MAX_2(1,99));
    printf("max_3:%d\n",MAX_3(1111,99,188));
    printf("max_4:%d\n",MAX_4(11,199,188,1));
    printf("max_5:%d\n",MAX_5(11,199,2000,188,15555));
}

输出结果
liu@ubuntu:~/code_test/c_test$ gcc define.c 
liu@ubuntu:~/code_test/c_test$ ./a.out 
a_size:1
b_size:4
max_2:99
max_3:1111
max_4:199
max_5:15555
liu@ubuntu:~/code_test/c_test$ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值