Thread: printf whitespace control

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    printf whitespace control

    Hi guys I need to now that I can control the white spaces in the printf("%5d") defined by 5.

    In my program I need to control it to implement or decrement. So instead of using %5 what can I use.

    I need ideas, thanks for help!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You can create the format string on the fly:
    Code:
    char format[100];
    sprintf(format, "%%%dd", 5); // or snprintf if available
    printf(format, some_int);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by man page
    A field width or precision, or both, may be indicated by an asterisk `*'
    or an asterisk followed by one or more decimal digits and a `$' instead
    of a digit string. In this case, an int argument supplies the field
    width or precision. A negative field width is treated as a left adjust-
    ment flag followed by a positive field width; a negative precision is
    treated as though it were missing. If a single format directive mixes
    positional (nn$) and non-positional arguments, the results are undefined
    Eg.
    Code:
    printf( "%*d\n", 5, some_int );
    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.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is this C or C++? This is C code, yet is posted in the C++ section.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    actually I am trying to control

    sprintf(*form, "%s", "%5d");

    in this line %5d must be controlled by a variable!

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    and it is c++ but we're not using its GUI

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    in this line %5d must be controlled by a variable!
    What are you doing? You have already been given code to accomplish exactly this.

    and it is c++ but we're not using its GUI
    What GUI? If you are using C++, then you can also do this the C++ way.
    Code:
    std::cout << std::setw(5) << i << std::endl;

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    sorry it is

    sprintf(form, "%s", "%5d");

    not *form

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    50
    Quote Originally Posted by komacan View Post
    and it is c++ but we're not using its GUI
    If you're using C++, why not use cout and iomanip's setw?

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    but this must be controlled by a variable "%5d" part must be controlled

    and also I am obligated to write in C but in c++ file

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you should read the man page on printf/sprintf/etc, and note how to use a variable to control the precision.

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    I ve already read but nothing founr so I am here!

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then you need to read better.

    Quote Originally Posted by man 3 printf
    A field width or precision, or both, may be indicated by an asterisk `*'
    or an asterisk followed by one or more decimal digits and a `$' instead
    of a digit string. In this case, an int argument supplies the field
    width or precision. A negative field width is treated as a left adjust-
    ment flag followed by a positive field width; a negative precision is
    treated as though it were missing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a C Programing Quiz project
    By dilemma in forum C Programming
    Replies: 12
    Last Post: 05-15-2009, 03:35 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM