Thread: Quick printf question

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41

    Quick printf question

    I think this is a very simple question but I haven't been able to find the answer in my book.

    This is how I want the output to look
    Code:
    00
    01
    02
    03
    ....
    but I am getting
    Code:
     
    0
    1
    2
    3
    ...
    my printf command looks like
    Code:
    printf("Process %2d of %d on %s\n", rank, size, name);
    I thought the 2 preceding the first d would force the width of the first printed int to be two digits wide. Apparently I am missing something. Thanks in advance.

    EDIT: I got it to work with this method
    Code:
    if (rank < 9) {
    		printf("Process %d%d of %d on %s\n", 0, rank, size, name);
    	}
    else {
    		printf("Process %2d of %d on %s\n", rank, size, name);
    	}
    but there has to be an easier way than this.
    Last edited by waterborne; 02-25-2010 at 12:17 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It CAN force that - does on my compiler, but you need to add a 0 before the 2.

    In any case, you have an error, since there should be three values printed per line, not one. Check that out and see what those values are.
    Last edited by Adak; 02-25-2010 at 12:23 AM.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    Just put a 0 in front of the % and you'll be good to go. I'm interested to see if there are any other ways.

    I tried doing doing x = 02; and printing it your way, but it didn't work either.

    I don't think he was copy/paste 'ing his output, just an example.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    Code:
    printf("Process %02d of %d on %s\n", rank, size, name);
    Kindly use like %02d in the printf as mentioned above in your code
    Last edited by thillai_selvan; 02-25-2010 at 12:22 AM.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    You should be getting an output of :
    Code:
     1
     2
     3
     ...
    10
    11
    All with a space in the beginning, if you want to pad it with zeros you have to say so.
    Code:
    printf("%02d"......

    Dylan

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    7
    Are you able to see 00, 01, 02 ... for "rank"?

  7. #7
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    Using %02d did exactly what I was looking for.

    There is no error in my program - I just did not post my program's output. I just put one column of numbers because those were the only ones I cared about for my question. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with two two dimensional arrays
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 12-06-2009, 06:57 PM
  2. Stuck with function
    By scmurphy64 in forum C Programming
    Replies: 9
    Last Post: 11-10-2009, 11:41 AM
  3. scanf and printf question
    By mmattson07 in forum C Programming
    Replies: 8
    Last Post: 10-11-2009, 01:31 PM
  4. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  5. Quick question
    By cmanrules in forum C Programming
    Replies: 6
    Last Post: 12-12-2008, 02:15 AM