unsigned long long overflow?

Discussion in 'C Programming' started by dmpk2k, Jul 9, 2003.

  1. dmpk2k

    dmpk2k Guest

    Forgive me if this is obvious to you. I feel somewhat foolish.

    I've scribbled together something that demonstrates my problem. It takes the
    product of all integers n squared, where n is 1..10.

    -----------------
    int main() {
    int i;
    unsigned long long product = 1;

    for (i=1; i<=10; i++) product *= i * i;

    printf("%U\n", product);

    return 0;
    }
    -----------------

    Would someone care to inform me why the output is 4114677760 and not
    13168189440000? sizeof() tells me that I'm getting a 64-bit variable, so
    what's going on? Is it an issue with printf()? And how do I resolve this?


    Thanks,

    dmpk2k
     
    dmpk2k, Jul 9, 2003
    #1
    1. Advertisements

  2. dmpk2k

    Kevin Easton Guest

    printf("%llu\n", product);
    - Kevin.
     
    Kevin Easton, Jul 9, 2003
    #2
    1. Advertisements

  3. dmpk2k

    Dan Pop Guest

    %U is wrong for *anything*, being plain undefined behaviour.

    Dan
     
    Dan Pop, Jul 9, 2003
    #3
  4. You want "%llu"
     
    Emmanuel Delahaye, Jul 13, 2003
    #4
  5. dmpk2k

    Dan Pop Guest

    Unless he has a conforming C99 implementation, there is no way of telling
    what he really needs.

    Dan
     
    Dan Pop, Jul 14, 2003
    #5
  6. dmpk2k

    Bill Hanna Guest

    MS Visual C++ and Bloodshed C++ have a bug in the ostream program
    that limits the output to 32 Bits when using printf. It will only
    output the lower half of the long long data. MS has listed a
    work-around fix on their website. I have tried to use their
    recommendation but it does not work. MS states that the bug will be
    fixed in Visual C.net.
    Bill Hanna
     
    Bill Hanna, Jul 18, 2003
    #6
  7. dmpk2k

    Bill Hanna Guest


    Use "%I64d" or "%I64u" or "%I64x". This works on Bloodshed C++.
    I found the info from an old posting in 2001.

    Bill Hanna
     
    Bill Hanna, Jul 18, 2003
    #7
  8. The Dev-C++ IDE 4.x by Bloodshed uses gcc 3.2.x. It supports C99 'long long'
    and "%llu".
     
    Emmanuel Delahaye, Jul 18, 2003
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.