Last active
December 21, 2020 08:50
-
-
Save ismdeep/6747473a7a9a6b81e58df191fb999a97 to your computer and use it in GitHub Desktop.
C 语言探测数值类型
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define type_name(expr) \ | |
(_Generic((expr), \ | |
char: "char", unsigned char: "unsigned char", signed char: "signed char", \ | |
short: "short", unsigned short: "unsigned short", \ | |
int: "int", unsigned int: "unsigned int", \ | |
long: "long", unsigned long: "unsigned long", \ | |
long long: "long long", unsigned long long: "unsigned long long", \ | |
float: "float", \ | |
double: "double", \ | |
long double: "long double", \ | |
void*: "void*", \ | |
default: "?")) | |
int main() { | |
/* 探测数值类型 */ | |
printf("%s\n", type_name(2L)); | |
printf("%s\n", type_name((2L + 4.5f))); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
利用宏定义探测数值类型