Thread: printing null values with printf

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    99

    printing null values with printf

    I want to print two distinct linked list both containing prize and size. THe result should look like this

    --------BUY------------------SELL-----------------
    200 1.22 100 2.00
    100 1.10 40 3.00
    300 4.90
    48 5.75

    I see two ways of doing this:
    - with one printf statement, something like
    printf("%5i %5f %5i %5f\n", buySize, buyPrice, sellSize, sellPrice);
    If I loop through both lists at a certain point one of the lists is empty, I need to keep printing the empty left values or otherwise the sell values outline to the left. How can I do this
    with two separate printf statements for both sets of colums but then I have the same issues.

    Any printf wizzards that know how to do this?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    When you run out of list on one side or the other, just set the respective values to 0...
    Better still, amalgamte the redundent lists and work from a single list.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    I don't want to print '0's, that would not be acceptable in this context. Merging the list won't solve it either because you would still have to deal with NULL values. BTW, I also tried something like:
    Code:
    printf("%5f:\n", (buyPrice == NULL ? NULL : buyPrice))
    but then the compiler starts to complain...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,984
    Something like
    Code:
    while ( buy != NULL && sell != NULL ) {
      char buySize[20] = { 0 }, sellSize[20] = { 0 };
      // ditto for other fields
      if ( buy ) {
        sprintf(buySize,"%d",buy->size);
        buy = buy->next;
      }
      // ditto sell
      printf("%20s %20s\n", buySize, sellSize );
    }
    If either string is empty, it will just be printed with padded spaces.
    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.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    I don't want to print '0's, that would not be acceptable in this context. Merging the list won't solve it either because you would still have to deal with NULL values. BTW, I also tried something like:
    Code:
    printf("%5f:\n", (buyPrice == NULL ? NULL : buyPrice))
    but then the compiler starts to complain...
    Merging the lists so that your values are all in a single struct is the best answer... Unused values set to 0 can be used as a sentinal value to control how things are printed...
    Code:
    struct tBuySell
      { float prices[4];
         struct tBuySell *next; }
    
    // display a price point
    for(i = 0; i < 4; i++)
      if (bsList->prices[i] > 0)
        printf("%.2f\t",bslist->prices[i]);
      else 
       printf("\t\t");
    Like it's hard?

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    \t does not result to anything on my windows machine..

    Guess I was hoping for some deep printf-secrets but appearantly there are not that many. For those who were not aware of it, you can make a variable of the length by putting '*' in the format string of printf like:
    Code:
    printf("%*f\n", margin, floatNumber)
    by varying the value of 'margin' you van decide where to start printing. This only works when you are right aligning, so for now I just choose to print a string of spaces. Thanks everybody.
    Last edited by django; 08-23-2011 at 08:17 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,984
    You could do this perhaps (then)
    printf("%*s%f", margin, "", myfloat );
    which would print an empty string, padded with as many spaces as necessary, then a regularly aligned float.
    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.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    \t most certainly does print a tab on a Windows machine, or at least moves you to the next tab stop (multiple of eight characters), so it's just a question of making sure your \t goes where you want it (and there is the fact that it's stuck to eight characters).

    If you want to make it left-aligned, you put a minus sign in.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by tabstop View Post
    \t most certainly does print a tab on a Windows machine, or at least moves you to the next tab stop (multiple of eight characters), so it's just a question of making sure your \t goes where you want it (and there is the fact that it's stuck to eight characters).
    Weird, does not work for me. Where is configured where the tabstop is placed?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by django View Post
    Weird, does not work for me. Where is configured where the tabstop is placed?
    It's configured somewhere in Redmond. I used
    Code:
    #include <stdio.h>
    
    int main(void) {
        printf("         111111111122222222223333333333\n");
        printf("123456789012345678901234567890123456789\n");
        printf("\tA tab\tAnother tab\tA tab after eight...\n");
    }
    and got
    Code:
             111111111122222222223333333333
    123456789012345678901234567890123456789
            A tab   Another tab     A tab after eight...

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    \t does not result to anything on my windows machine..

    Guess I was hoping for some deep printf-secrets but appearantly there not that many. For those who were not aware of it, you can make a variable of the length by uning '*' in the format string of printf like
    Code:
    printf("%*f\n", margin, floatNumber)
    by varying the value of 'margin' you van decide where to start printing. This only works when you are right aligigning, so for now I just choose to print a string of spaces. Thanks everybody.
    \t is a tab (like pressing the tab key on your keyboard) used to align data into columns.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by Salem View Post
    You could do this perhaps (then)
    printf("%*s%f", margin, "", myfloat );
    which would print an empty string, padded with as many spaces as necessary, then a regularly aligned float.
    Yes, that works, thanks.

    The tabstops work too, you should place them before the variable and not after...

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    Yes, that works, thanks.

    The tabstops work too, you should place them before the variable and not after...
    The placement of tabstops is dependent on how you want to align your text... for example: if you are working in a loop, putting it before the variable will indent the first column... if you don't want that place it after.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing null pointers
    By SterlingM in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2010, 06:04 PM
  2. printf if not null with struct and typedef
    By kbrandt in forum C Programming
    Replies: 10
    Last Post: 05-15-2009, 12:50 PM
  3. linked list problems (null values)
    By scwizzo in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2008, 06:04 PM
  4. How to printf NULL
    By g_p in forum C Programming
    Replies: 5
    Last Post: 04-12-2008, 11:19 AM
  5. Printing problem when I use NULL
    By samc2004 in forum C Programming
    Replies: 3
    Last Post: 12-10-2003, 08:32 PM