0

So I need to print for example a name and then an id (John 234543 ) where the name has 6 spaces for it and the id 7. So in this example I would do printf("%6s %d", "John", 234543); but I cant just leave my code as that because of the input is a 3 character after adding the spaced the id will move less than it should.

Ive tried with ("%-#s") and ("%#s") and ive tried like ("%%ds", int, "john"); where i was hoping the int could add there making the spaced but it didn't work

printf("%%ds", int, "john");

The output never works for every case just the one of them where the amount of characters is just right.

Diego Esquivel
  • 182
  • 1
  • 12
  • 1
    You should provide some samples, actual output and desired outputs. – CristiFati Sep 01 '19 at 20:20
  • Ok what i need to know is that if theres a way to do a printf("%#s" , string) where my # is a number that changes depending on the input i get. For example if I need 5 spaces that # can be a variable or something that has a 5 in it but of in the next time i input something it needs 6 spaces that # can be a 6 instead. Like if I can change the spaces on my printf – Diego Esquivel Sep 01 '19 at 20:28
  • 2
    Duplicate of [Set variable text column width in printf](https://stackoverflow.com/questions/7105890/set-variable-text-column-width-in-printf) – Marco Bonelli Sep 01 '19 at 20:46
  • See also [Is there a way to specify how many characters of a string to print out using `printf()`?](https://stackoverflow.com/questions/2239519/). – Jonathan Leffler Sep 01 '19 at 21:20

0 Answers0