1

I am working on a project for my C Programming Class. I know that when specifying width in a printf statement WITH a variable, it would look like this:

printf("%03d\n", tvQuantity);

With 3 being the width and 0 stating we want the number preceded by 0's.

My question is, how would I define a width in a printf statement where I'm not calling a variable? For example, to add a width of 6 to the following statement:

printf("Text here");

As there is no format specifier (such as %d), where would I place my width and other formatting arguments?

5
  • What would you expect a width of 6 to do there? Commented Jan 30, 2020 at 20:20
  • @Schwern I'm trying to output my data in a format similar to this imgur.com/a/Vb6PQCw Commented Jan 30, 2020 at 20:22
  • @RobertRDev So you do have variables? Commented Jan 30, 2020 at 20:25
  • Yes just not for the QTY, DESCRIPTION, UNIT PRICE, AND TOTAL PRICE column names Commented Jan 30, 2020 at 20:30
  • @RobertRDev Post data here, not behind something that may be inaccessible. Commented Jan 30, 2020 at 20:33

1 Answer 1

2

printf("Text here"); needs no width. It'll just print Text here.

You could write it as printf("%6s", "Text here") but that will do nothing as the width is a minimum and the text is longer than 6 characters.

If you want to pad the text, say with a width of 20, you can write printf("%20s\n", "Text here"). This could be useful if the string was defined in a constant.

To format your text in columns, you'd use multiple %s formats in a single printf.

printf("%20s %20s %20s\n", "Cell 1", "Cell 2", "Cell 3");
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was searching for. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.