typeof和clamp

                      (1)        typeof 不是 typedef 
                 

                (2)

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>


#define clamp(val,min,max) ( { \
typeof(val) __val = (val); \
typeof(val) __min = (min); \
typeof(val) __max = (max); \
__val = __val < __min ? __min:__val;\
__val > __max ? __max:__val;})

#define IS_DOMAIN(val,min,max) (((val>=min)&&((val<=max))?true:false))

int main(void)
{

int value = 500;
float ad_value = 100.00;
printf("value is %d\n",IS_DOMAIN(value,0,50));
printf("value is %d\n",value);

printf("is_down is %f\n",(double)IS_DOMAIN(ad_value,0,50));
printf("value is %f\n",(double)value);

value = clamp(value,0,50);
printf("value is %d\n",value);
value = clamp(value,1U,10);
printf("frame index is %d\n",value);


return 0;
}

value is 5
frame index is 5

 

posted @ 2020-11-11 11:31  卷哭你  阅读(169)  评论(0编辑  收藏  举报