I want to print text to a terminal program from a microcontroller, something like this:
printString("textline one here: OK\n"); printString("textline two here that is longer: OK\n"); printString("textline three here that is even longer: OK\n"); How do I make the text to always be in columns even if I decide to change the textline? To avoid that it looks something like this in the printout in the terminal program:
textline one here: OK textline two here that is longer: OK textline three here that is even longer: OK and more like this (without having to add extra spaces in text and double check in the terminal program how it looks for every change I do to any text) :
textline one here: OK textline two here that is longer: OK textline three here that is even longer: OK Is it easier to use printf or printstring for this?
printstringis not a standard C function.printf("%-80s%s\n", "text one line", "OK")printf("%-75s %s\n", message, status);so that even if the message is longer than the specified length (75 here), you get separation between the string and the status. Alternatively, tellprintf()to truncate the message:printf("%-75.75s %s\n", message, status);. You could change the lengths dynamically; replace the75values with*and pass anintargument before the value:printf("%-*.*s %s\n", 75, 75, message, status);.