Newbie data types question in printf vs iostream

Discussion in 'C++' started by Mark Bruno, Dec 26, 2003.

  1. Mark Bruno

    Mark Bruno Guest

    When I declare a 64 bit integer (long long or __int64), it seems to have the
    same boundaries as a 32 bit int while using printf, but the correct 64 bit
    boundaries when using cout. Here's my code:

    #include <cstdio>
    #include <iostream>
    using namespace std;
    int main()
    {
    long long a = 2147483647; //The max value for a signed 32 bit int
    long long b = 9223372036854775807; //The max value for a signed 64 bit int
    printf("%d %d %d\n", a, a+1, b);
    cout << a << " " << a+1 << " " << b << endl;
    return 0;
    }
     
    Mark Bruno, Dec 26, 2003
    #1
    1. Advertisements

  2. Mark Bruno

    Arthur Gold Guest

    Because that's what you're *telling* it to do; `%d' is for printing
    `int'. The format specifier you want is `%lld' for long long.

    [The C++ stream operators get it `right' because of overloading.]

    HTH,
    --ag
     
    Arthur Gold, Dec 26, 2003
    #2
    1. Advertisements

  3. Mark Bruno

    Steven Green Guest

    A 64bit noob here.
    Is long long standard or is it really just long.

    For g++ the compiler will treat long as a 64bit int if compiled with
    the -m64 option (although my compiler hasn't been compiled to support it)

    I would guess that the printf function doesn't know anything about a
    long long and is merely treating it as though it were a 32bit int.
     
    Steven Green, Dec 26, 2003
    #3
  4. Mark Bruno

    Arthur Gold Guest

    Good catch.

    The type `long long' became standard in C99; it is not part of standard
    C++ (though it's likely to become the case at some point). In any event,
    it's an extension that's frequently available.
    See my reply elsethread.

    HTH,
    --ag
     
    Arthur Gold, Dec 26, 2003
    #4
  5. Mark Bruno

    Mark Bruno Guest

    D'OH!!! I forgot to use %llu!!! I'm so stupid! Thanks for pointing out my
    error.
     
    Mark Bruno, Dec 26, 2003
    #5
  6. Mark Bruno

    Jack Klein Guest

    Jack Klein, Dec 26, 2003
    #6
    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.