我尝试在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
有谁知道错误的原因?
答案 0 :(得分:1)
你需要有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" : "")