此问题在此处已有答案:
Character Constants and Initialization in C(2个答案)
昨天关门了。
我希望_Generic适用于char数据类型:
#include <stdio.h>
#define test(x) _Generic((x), \
char: 0, \
int: 1, \
double: 3, \
default: 4 \
)
int main()
{
printf("%c\n", test('c')); // Result was ☺ character so I switched to %d
printf("%d\n", test('c')); // 1, which I would have received if 'c' was an int but 'c' is a char
return 0;
}
顺便说一下,我知道字符在技术上是整型的,我只是想知道是否有什么方法可以修复这个问题,所以它打印0代替。
如果需要有关_Generic的信息,请使用以下链接:http://www.robertgamble.net/2012/01/c11-generic-selections.html
1条答案
按热度按时间a0x5cqrl1#
这是因为char常量的类型为
int
,如果将其强制转换为char,它将按预期工作https://godbolt.org/z/Mea1v8jv6