我正在尝试为我未来的项目用C语言编写一个基于宏的终止字符串着色程序。到目前为止,我得到的信息是:
#define ANSI_RED "\x1b[31m"
#define ANSI_GREEN "\x1b[32m"
#define ANSI_YELLOW "\x1b[33m"
#define ANSI_BLUE "\x1b[34m"
#define ANSI_MAGENTA "\x1b[35m"
#define ANSI_CYAN "\x1b[36m"
#define ANSI_RESET "\x1b[0m"
#define ANSI_COLOR(color, string) color string ANSI_RESET
#define FOREGROUND 38
#define BACKGROUND 48
#define RGB_COLOR(plane, r, g, b, string) "\033[" plane ";" r ";" g ";" b "m" string ANSI_RESETANSI_COLOR宏工作得很好,但当我尝试使用RGB_COLOR宏时,如下所示:
printf( RGB_COLOR(FOREGROUND, 248, 42, 148, "Starting the server:\n") );我得到一个错误:
/c-http-server/main.c:17:23: error: expected ')'
printf( RGB_COLOR(FOREGROUND, 248, 42, 148, "Starting the server:\n") );
^
/c-http-server/libs/c-chalk/chalk.h:11:20: note: expanded from macro 'FOREGROUND'
#define FOREGROUND 38
^
/c-http-server/main.c:17:11: note: to match this '('
printf( RGB_COLOR(FOREGROUND, 248, 42, 148, "Starting the server:\n") );我已经在SO上找到了这个问题,大多数解决方案都是关于找到额外的')',但我在我的代码中找不到一个。
如果有人能帮我找出问题所在,我会很高兴的,也许我只是看不见,错过了一些明显的东西。
发布于 2019-03-07 00:43:21
看起来您正在尝试连接字符串和整数,这是不可能的。
作为一种快速解决方法,您可以尝试
#define FOREGROUND "38"
#define BACKGROUND "48"并像这样使用它
printf( RGB_COLOR(FOREGROUND, "248", "42", "148", "Starting the server:\n") );另一方面,应该可以(更干净地)对参数进行stringize (未测试):
#define xstr(a) str(a)
#define str(a) #a
#define RGB_COLOR(plane, r, g, b, string) "\033[" str(plane) ";" str(r) ";" str(g) ";" str(b) "m" string ANSI_RESET请注意通过xstr和str的绕道,因为正如@John Bollinger正确注释的那样,串化会阻止宏扩展。
发布于 2019-03-07 01:28:20
当你连接字符串时,你可以像"aa" "bb"那样做,结果就像写"aabb"一样。
因此,当您调用printf(参数)时,它允许这样写
printf("str1" "str 2")但是,如果要调用printf将字符串参数与数字组合在一起,则必须在参数之间加逗号
printf("aa" "bb", 100)否则,解释器将认为您到达了最后一个参数--在下面这样的情况下
printf("aa" "bb" 100)它会认为你应该在100之前结束括号。
在您的示例中,您尝试调用
printf( RGB_COLOR(38,248,42,148,“启动服务器:\n”) );
它被重写为
printf( "\033[" "Starting the server:\n" ";" 248 ...... )这意味着
printf( "\033[Starting the server:\n;" 248 ...... )
^->HERE EXPECTS COMMAhttps://stackoverflow.com/questions/55028007
复制相似问题