使用gcc.exe(Rev3,由MSYS2项目构建)8.2.0。
我试图建立一个宏来自动进行两种类型之间的类型转换,其中两个参数绝不能是同一类型。我的问题是,如果我也不包含相同类型的大小写,则编译器将引发错误。我想要的是:
#include <stdio.h>
#include <stdint.h>
// Macro to return string based on two different types
#define bob( to, from ) \
_Generic( to , \
int32_t: _Generic(from, \
int16_t: "s-l", \
int8_t: "c-l" ) , \
int16_t: _Generic(from, \
int32_t: "l-s", \
int8_t: "c-s") , \
int8_t:_Generic(from, \
int32_t: "l-c", \
int16_t: "s-c") \
)
void main(void)
{
int32_t i1;
int16_t s1;
int8_t c1;
printf("%s\n", bob(i1,s1));
printf("%s\n", bob(i1,c1));
printf("%s\n", bob(s1,c1));
printf("%s\n", bob(s1,i1));
printf("%s\n", bob(c1,s1));
printf("%s\n", bob(c1,s1));
}
Run Code Online (Sandbox Code Playgroud)
$ gcc …Run Code Online (Sandbox Code Playgroud) 我正在处理一些使用 C99 编译器编译的正在运行的遗留 C 代码,我在头文件中间的空行上遇到了一个“#”(哈希符号)。我使用 -Wall、-Werrors 和 -pedantic 进行编译,但没有收到运行我的单元测试的 GCC 编译器或我的目标交叉编译器的投诉。我对语言和预处理器的 BNF 语法进行了快速的谷歌搜索,但无法弄清楚流浪 # 会发生什么。它碰巧在第 1 列,但如果我将它移到第 2 列,它仍然不会发出任何警告。我的 GCC 编译器是 gcc(Rev1,由 MSYS2 项目构建)9.1.0。
真的不是问题,只是出乎意料。
有人有任何见解吗?