0

I'm trying to align multiple variables in a table like output. I am currently using padding but I can not see how to correctly align the output. I print the variables:

printf("\n %15s", name); printf("%15d", level); 

Then I call the next function repeatedly, all is fine unless there is more than one digit.

printf("%11dhr %.1fmn",t,t2); 

enter image description here

2
  • @nnn is this to go into the printf function that prints t and t2/ Commented Dec 4, 2015 at 21:27
  • @nnn thank you, could you please post this as an answer? Commented Dec 4, 2015 at 21:27

1 Answer 1

1

Having the variables defined as int t; and float t2; (or double t2;), as it is now %.1f prints t2 with one digit after the decimal point. This is the precision specifier.

For alignment, you want also to include the width specifier (minimum number of characters to be printed, padded with spaces if less are available). This should include the decimal point and the precision digits. So if the integer part of t2 will fit when printed in 2 characters, you will need to set the width to 4. For the given example, this should work:

printf("%11dhr %4.1fmn", t, t2); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.