Thread: printf() bug? or..?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    11

    printf() bug? or..?

    Hello

    could someone please explain this odd behavior of a printf() funtion:

    Code:
    printf("=%lu =%lu\n", 11111111111, 11111111111); // Output: =2521176519 =2
    Thank you.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    11
    Don't know exactly what you mean. If you mean
    Code:
    printf("=%u =%u\n", 11111111111UL, 11111111111UL);
    or
    Code:
    printf("=%lu =%lu\n", 11111111111, 11111111111);
    well neither work.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Compiler won't promote type for variadic function like printf.
    Thus passing 1233423 means it's just integer for printf. You need to explicitly say the type (either cast or suffix will do for numbers).

    E.g
    printf("%f \n", 3 ); // won't work pass int. printf expects double.

    You need to be consistent!!!!
    Try

    printf("=%lu =%lu\n", 11111111111UL, 11111111111UL);


    Basic C program help
    I think this kind of question is quite common.
    Last edited by Bayint Naung; 05-24-2011 at 06:58 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Those constants are too big, even for unsigned long.

    Try
    Code:
    $ gcc -Wall bar.c
    $ ./a.out 
    =11111111111 =11111111111
    $ cat bar.c
    #include<stdio.h>
    
    int main ( ) {
      printf("=%llu =%llu\n", 11111111111ULL, 11111111111ULL);
      return 0;
    }
    Of course, you need a real C99 library to be able to deal with this properly.
    If you're stuck with some fossil compiler, or are crippled by the MS runtime library that all the MinGW ports rely upon, you're pretty much stuck.
    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.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    11
    Code:
    printf("=%lu =%lu\n", 11111111111UL, 11111111111UL);
    This does not work.

    But this:
    Code:
    printf("=%llu =%llu\n", 11111111111ULL, 11111111111ULL);
    works like a charm, even if I input something like:
    Code:
    printf("=%llu =%llu\n", 111111111111111111111111111111111111111111111ULL, 11111111111ULL);
    The second number is correct, which is really my issue, I don't have the problem with printing BIG ARSE :P numbers, my problem was that the first number obviously somehow truncated the second one, and that was my real problem. But this works like a fairy tale.

    BTW can anybody explain, why and how this works.

    Thanks.

    EDIT: I'm using Pelles C

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juvan View Post
    Code:
    printf("=%lu =%lu\n", 11111111111UL, 11111111111UL);
    This does not work.

    But this:
    Code:
    printf("=%llu =%llu\n", 11111111111ULL, 11111111111ULL);
    works like a charm, even if I input something like:
    Code:
    printf("=%llu =%llu\n", 111111111111111111111111111111111111111111111ULL, 11111111111ULL);
    The second number is correct, which is really my issue, I don't have the problem with printing BIG ARSE :P numbers, my problem was that the first number obviously somehow truncated the second one, and that was my real problem. But this works like a fairy tale.

    BTW can anybody explain, why and how this works.

    Thanks.

    EDIT: I'm using Pelles C
    It works because in PellesC unsigned long long is a 64 bit integer... thus ULL and %llu will accept values up to 64 bits...

    Thus you have a range from 0 to 18,446,744,073,709,551,614

    You can look all this up in your Pelles C help file ...
    Open Poide click Help -> Contents ->C99 language reference->Declarations and types->Storage of basic types.

    ( A) Pelles C is probably the best documented compiler available B) why don't people ever look in help files on their own?)

    It's probably a lot clearer to #include <stdint.h> and specify ... uint64_t ... for your variables.

    However; your second one with that monster row of 1s should not work even there... If you have to do anything that produces numbers that big you should be working with one of the "bignum" libraries. (Google is your friend)
    Last edited by CommonTater; 05-24-2011 at 07:45 AM.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    11
    As I said, I don't care about printing large numbers and getting to see the right values, my problem was, when I at first said:
    Code:
    printf("=%lu =%lu\n", 11111111111, 11111111111); // Output: =2521176519 =2
    And as you can see the second number got ruined because obviously the first one wrote over it. And that was my problem, but now with
    Code:
    %llu
    it leaves the second number alone.

    Well I may now enjoy some relaxation

    Thanks for all your help guys.

    EDIT: Since I am new here, is there any way to mark this thread as "SOLVED" ?
    Last edited by juvan; 05-24-2011 at 07:52 AM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juvan View Post
    And as you can see the second number got ruined because obviously the first one wrote over it. And that was my problem, but now with %llu it leaves the second number alone.
    No, that's not what happened... you provided values that caused errors, the result is undefined. That means printf() displayed garbage. nothing was over-written or ruined. ... again READ THE HELP FILE... it's all there.

    You are using a compiler that comes with a 1.2mb help file... do you think it might be helpful to actually read it?

    Here's a little trick most people miss... put your text cursor on any keyword or library function call... Press F1... Oh my!

    If you go to the Pelles C forums you can download a copy of the Windows SDK that will integrate into Pelles C and provide you documentation on basic Windows API calls with F1 as well...
    Last edited by CommonTater; 05-24-2011 at 07:58 AM.

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    11
    A) Yes it really is B) It's hard to look if you don't know what you're looking for.

    In the HELP FILE: long long, unsigned long long | 8 bytes | 8 bytes | [3.00] 8 bytes

    This was the only thing on the page that had any relevance with my problem, and I myself couldn't have figured out anything from that alone.

    Could you please tell me where all of this is said, cuz in the help files alone I see no sign of it.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think the problem is that you think there's something to figure out. If you lie to printf (by passing in arguments that don't match the format), it will not print things that you want to see. When the things you pass are of different sizes then what you tell printf, then it will get out of alignment.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    11
    If you go to the Pelles C forums you can download a copy of the Windows SDK that will integrate into Pelles C and provide you documentation on basic Windows API calls with F1 as well...
    I was searching with no success, would you perhaps have a link?

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juvan View Post
    A) Yes it really is B) It's hard to look if you don't know what you're looking for.

    In the HELP FILE: long long, unsigned long long | 8 bytes | 8 bytes | [3.00] 8 bytes

    This was the only thing on the page that had any relevance with my problem, and I myself couldn't have figured out anything from that alone.

    Could you please tell me where all of this is said, cuz in the help files alone I see no sign of it.
    What you can't figure out how big a number fits into 8 bytes? Any simple calculator will tell you that... 8 bytes x 8 bits = 64 bits .. 2^64 =

    In fact the number I gave you above came from exactly that calculation in windows standard calc.exe application, which I added to my PellesC tools menu for convenience...

    Anyway... put your typing cursor on printf and press F1 ... notice that in the resulting brief page fprintf is a link... click the link and VOILA.

    Really ... juvan... Since the day you installed Pelles C all this information has been right there for you to explore. WHY WOULD YOU IGNORE IT???

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    11
    Nice of you to help, but now you're just patronizing. So let's just end the discussion.

    HF.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juvan View Post
    I was searching with no success, would you perhaps have a link?
    Oh for crying out loud... Open POIDE... click Help -> PellesC on the Web-> Forum then search for win98sdk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with printf
    By f4ichick02 in forum C Programming
    Replies: 4
    Last Post: 03-11-2009, 05:58 AM
  2. Help with printf
    By elliptic in forum C Programming
    Replies: 3
    Last Post: 09-13-2006, 11:03 PM
  3. make printf using printf?
    By germaneater in forum C Programming
    Replies: 9
    Last Post: 11-10-2004, 10:58 PM
  4. printf
    By Jaguars in forum C Programming
    Replies: 1
    Last Post: 04-17-2003, 09:20 AM
  5. about printf
    By unregistered in forum C Programming
    Replies: 2
    Last Post: 04-11-2002, 12:37 PM