String literal for a colored readline with a string variable

Is it possible to put a variable text_infoin readline among colors?
The GCC debugger gives me this error:" error: expected ‘)’ before 'text'

#include <readline/readline.h>

#define CYELLOW "\001\e[0;31m\002"
#define RESET   "\001\e[0m\002"

int main(int argc, char **argv)
{
    char *text_info = "its very simple string";
    readline(CYELLOW text_info RESET);
    return 0;
}

I know the below works, but it is not.

int main(int argc, char **argv)
{
    readline(CYELLOW "simple string" RESET);
    return 0;
}
+4
source share
2 answers

The line you posted does not work because it cannot be concatenated during assembly:

readline(CYELLOW text_info RESET);

Since @Weaterh Vane is already mentioned in the comment above, the only real solution is to build a line at runtime through sprintfor better snprintf.

 char aBuffer[100];
 snprintf(aBuffer, sizeof(aBuffer), "%s%s%s", CYELLOW, text_info, RESET);

: , , aBuffer ( text_info). 100 .

+1

readline(CYELLOW text_info RESET);

-

readline("\001\e[0;31m\002" "simple string" "\001\e[0m\002");

, ,

readline("\001\e[0;31m\002simple string\001\e[0m\002");

. , .

readline(CYELLOW text_info RESET);

-

readline("\001\e[0;31m\002" text_info "\001\e[0m\002");

(.. , , ) - .

, ( ).

, ( "string", "C- , , ), ​​, .

Weather Vane, sprintf(), , .

+1

Source: https://habr.com/ru/post/1691891/


All Articles