Thread: printf format specifier padding

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    34

    printf format specifier padding

    I want to use padding when i print stuff.

    The normal way is

    Code:
    printf("%5s",string)
    But I want the number 5 to be replaced by a variable, as i want the padding to vary based on that variable.

    But I don't think I can just put the variable name where 5 is.

    Is there a way of using variable padding length?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read the manual - study the * width.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    Thanks. I was getting sscanf and printf mixed up.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I once had to use an old, broken version of sprintf() that didn't do the * correctly. Solution? First use sprintf() to build the format string, then use sprintf() again to format the output.

    Code:
    sprintf(fmt, "%%%dd", width);
    sprintf(str, fmt, val);
    Uh... Yay?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. I have some questions :(
    By geekrockergal in forum C Programming
    Replies: 19
    Last Post: 02-01-2009, 09:44 AM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  4. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  5. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 09:25 PM