Formatting with printf

This is a discussion on Formatting with printf within the C Programming forums, part of the General Programming Boards category; Let's say I want to right justify the int 'number' 5 spaces to the right. I put something like this: ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    Formatting with printf

    Let's say I want to right justify the int 'number' 5 spaces to the right. I put something like this:

    Code:
    printf("%5d",number);
    right?

    But what if I want to justify it a variable number of spaces to the right? How would I do that?

    Thanks!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    It's been awhile, let me see if I still remember:
    Code:
    printf("%*d",your_variable_number_of_spaces,number);
    Set "your_variable_number_of_spaces" to however many spaces you need.

    ...I think that's right.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    OK Now I am having a really big problem. I haven't been able to test that because the compiler is not working. I have reinstalled MinGW, and it doesn't work. I enter the command and it just gets stuck. I am so frustrated right now because I don't have the smallest idea of what the hell is happening.

    It has been happening since I reinstalled XP Pro on my computer, but atleast I get it to work after trying two or three times. Now it is not working at all.

    Then, when I try to close the command prompt because obviously nothing is happening, it tells me that the program is not responding and it asks me if I should end it now or cancel.

    Does anybody have any idea of what is happening? I tried closing a lot of running processes to see if one of them is the problem, but apparently not.

    I hate this computer.

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Quote Originally Posted by hk_mp5kpdw View Post
    It's been awhile, let me see if I still remember:
    Code:
    printf("%*d",your_variable_number_of_spaces,number);
    Set "your_variable_number_of_spaces" to however many spaces you need.

    ...I think that's right.
    Yep, that is one way to do it. But just doing:
    Code:
    printf("%5d", number);
    will do the same thing

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Thanks a lot! I still haven't been able to compile it!!! This computer is taking a vacation.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    So you enter the command to compile the program, say
    Code:
    gcc program.c -o program.exe
    and then it just hangs? The command prompt doesn't reappear? [edit] I mean the "C:\>" thing, not the black window. [/edit]

    Or are you using Dev-C++?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    hello dwks.

    Yes, it just hangs if I use that kind of command. It also hangs if I use make. And it also hangs if I use MSYS, the prompt just does not appear, it is if it was still trying to compile.

    If I say "gcc file.c", then it just hangs there in primes.c
    If I say "make" with a make file that says inside "gcc file.c", then it just hangs at "gcc file.c"

    Then, when I close the command prompt window, it says that it is not responding. If I use Ctrl+C, then gcc aborts and the prompt appears (C:\>), but when I close the command prompt window, it says that it is not responding also. And when I try multiple times to compile (using Ctrl + C when it is not successful) then when I close the command prompt it will tell me the number of times I used Ctrl+C that it is not responding.

    Yes, I know, confusing.

    But I think I managed it. I thought of the biggest change on my system the last couple of weeks, and so, I uninstalled COMODO firewall. Now gcc is working ok.

    [edit]
    Oh and P.S.: I tried the printf code, it works. Thanks!
    Last edited by samus250; 06-13-2008 at 07:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 05:28 PM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

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