printf format specifier padding

This is a discussion on printf format specifier padding within the C Programming forums, part of the General Programming Boards category; I want to use padding when i print stuff. The normal way is Code: printf("%5s",string) But I want the number ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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, 08:48 PM
  2. I have some questions :(
    By geekrockergal in forum C Programming
    Replies: 19
    Last Post: 02-01-2009, 08:44 AM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21