Skip to content

Instantly share code, notes, and snippets.

@ismdeep
Last active December 21, 2020 08:50
Show Gist options
  • Save ismdeep/6747473a7a9a6b81e58df191fb999a97 to your computer and use it in GitHub Desktop.
Save ismdeep/6747473a7a9a6b81e58df191fb999a97 to your computer and use it in GitHub Desktop.
C 语言探测数值类型
#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;
}
@ismdeep
Copy link
Author

ismdeep commented Dec 21, 2020

利用宏定义探测数值类型

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment