代码
提交代码
#include <stdio.h> #define typename(x) _Generic((x), /* Get the name of a type */ \ \ _Bool: "_Bool", unsigned char: "unsigned char", \ char: "char", signed char: "signed char", \ short int: "short int", unsigned short int: "unsigned short int", \ int: "int", unsigned int: "unsigned int", \ long int: "long int", unsigned long int: "unsigned long int", \ long long int: "long long int", unsigned long long int: "unsigned long long int", \ float: "float", double: "double", \ long double: "long double", char *: "pointer to char", \ void *: "pointer to void", int *: "pointer to int", \ default: "other") int main() { int a=1,b=2; float c=3.14159,d=0; printf("a type: %s, b type: %s, c type: %s, d type: %s\n",typename(a),typename(b),typename(c),typename(d)); a=(float)b+(float)c; printf("a=b+c, a=%d\n",a); printf("type (b+c): %s\n",typename((int)b+(int)c)); d=(int)b+(int)c; printf("d=b+c, d=%f\n",d); return 0; }#include <stdio.h> #define typename(x) _Generic((x), /* Get the name of a type */ \ \ _Bool: "_Bool", unsigned char: "unsigned char", \ char: "char", signed char: "signed char", \ short int: "short int", unsigned short int: "unsigned short int", \ int: "int", unsigned int: "unsigned int", \ long int: "long int", unsigned long int: "unsigned long int", \ long long int: "long long int", unsigned long long int: "unsigned long long int", \ float: "float", double: "double", \ long double: "long double", char *: "pointer to char", \ void *: "pointer to void", int *: "pointer to int", \ default: "other") int main() { int a=1,b=2; float c=3.14159,d=0; printf("a type: %s, b type: %s, c type: %s, d type: %s\n",typename(a),typename(b),typename(c),typename(d)); a=(float)b+(float)c; printf("a=b+c, a=%d\n",a); printf("type (b+c): %s\n",typename((int)b+(int)c)); d=(int)b+(int)c; printf("d=b+c, d=%f\n",d); return 0; }
运行结果