基础概念
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$