我正在尝试为我的 C 程序创建一个宏,以便在 printf 中使用
#define TIME(t) \
(t->tm_hour >= 6 && t->tm_hour < 12) ? "Good morning":"" && \
(t->tm_hour >= 12 && t->tm_hour < 18) ? "Good afternoon":"" && \
(t->tm_hour >= 18 && t->tm_hour < 23) ? "Good night":""
printf函数与下面相同
printf("%s\n", TIME(t));
编译中向我返回警报 C4474
C4474: too many arguments passed for format string
有谁知道这个错误的原因吗?
最佳答案
您需要有 else-conditions 级联:
#define TIME(t) \
(((t)->tm_hour >= 6 && (t)->tm_hour < 12) ? "Good morning" : \
((t)->tm_hour >= 12 && (t)->tm_hour < 18) ? "Good afternoon" : \
((t)->tm_hour >= 18 && (t)->tm_hour < 23) ? "Good night" : "")
关于根据时间创建消息宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47744391/