This question already has an answer here:
- sprintf() with automatic memory allocation? 5 answers
Is there a function like printf that can return a string instead of printing it? I have a function that prints a string in a certain color, but it has to be a string literal instead of accepting variables like printf.
// Function declaration (Assums YELLOW and NORMAL are the unix constants for terminal colors
void pYellow(char *str) {
printf("%s%s%s", YELLOW, str, NORMAL);
}
//Function call
void pYellow("This is a string");
If I wanted to print in color with a variable, it wont work. Like pYellow("Num: %d", 42);
will give an error, because its got too many parameters. And doing pYellow(printf("String"));
won't work either.
TL:DR I want to know if there's a printf method that returns a string instead of printing it.