2

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?

5
  • 3
    printstring is not a standard C function. Commented Jan 15, 2020 at 17:13
  • 6
    Use a fixed width. printf("%-80s%s\n", "text one line", "OK") Commented Jan 15, 2020 at 17:15
  • The section here on format placeholders has some useful information about the flags you can use to format your string: en.wikipedia.org/wiki/Printf_format_string Commented Jan 15, 2020 at 17:17
  • 2
    I'd suggest leaving a couple of spaces between the conversion specifications in 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, tell printf() to truncate the message: printf("%-75.75s   %s\n", message, status);. You could change the lengths dynamically; replace the 75 values with * and pass an int argument before the value: printf("%-*.*s   %s\n", 75, 75, message, status);. Commented Jan 15, 2020 at 17:32
  • Thank you all so much for your help!! really appreciate it. Commented Jan 21, 2020 at 16:22

1 Answer 1

2

Rather than including the first part of the text directly in your printf format string, pass it as a parameter to the %s format specifier. You can then add a field width to it to specify the minimum number of characters to print as well as the - flag to tell it to left-justify.

For example, this code:

printf("%-50s %s\n", "textline one here:", "OK"); printf("%-50s %s\n", "textline two here that is longer:", "OK"); printf("%-50s %s\n", "textline three here that is even longer:","OK"); 

Prints:

textline one here: OK textline two here that is longer: OK textline three here that is even longer: OK 

Also, you could use * instead of an a explicit field width to pass it in as a parameter. That way if you need to change the column width you only do it in one place:

int width = 50; printf("%-*s %s\n", width, "textline one here:", "OK"); printf("%-*s %s\n", width, "textline two here that is longer:", "OK"); printf("%-*s %s\n", width, "textline three here that is even longer:","OK"); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you all so much for your help!! really appreciate it.
@mannen Glad I could help. Feel free to accept this answer if you found it useful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.