C实现的泛型
程序代码:
#include <stdio.h> #define PRINT(type,x,format) do{printf("%"format,(type)x);}while(0) int main(void) { PRINT(int,20,"d"); return 0; }
#include <stdio.h> #define PRINT(type,x,format) do{\ type tmp=x;\ printf("%"format,(type)tmp);}\ while(0) int main(void) { PRINT(char*,"hello","s\n"); //PRINT(char,"hello error","s\n");//编译产生警告 PRINT(double,'c',"c\n"); PRINT(int,'c',"c\n"); //PRINT(int,999999999999999,"f\n");//编译产生警告 PRINT(double,999999999999999,"f\n"); return 0; }