_Generic关键字在C吗?1:20
C中的_Generic关键字用于为不同的数据类型定义MACRO。在C11标准版本中,此新关键字已添加到C编程语言中。_Generic关键字用于帮助程序员以更有效的方式使用MACRO。
此关键字根据变量的类型转换MACRO。让我们举个例子
#define dec(x) _Generic((x), long double : decl, \ default : Inc , \ float: incf )(x)
上面的语法是如何将任何MACRO声明为不同方法的泛型。
让我们以示例代码为例,该代码将定义一个MACRO,该MACRO将基于数据类型返回值-
示例
#include <stdio.h>
#define typecheck(T) _Generic( (T), char: 1, int: 2, long: 3, float: 4, default: 0)
int main(void) {
printf( "passing a long value to the macro, result is %d \n", typecheck(2353463456356465));
printf( "passing a float value to the macro, result is %d \n", typecheck(4.32f));
printf( "passing a int value to the macro, result is %d \n", typecheck(324));
printf( "passing a string value to the macro, result is %d \n", typecheck("Hello"));
return 0;
}输出结果
passing a long value to the macro, result is 3 passing a float value to the macro, result is 4 passing a int value to the macro, result is 2 passing a string value to the macro, result is 0
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短