How to print 64 bit integer using sprintf() family

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jannlin
    New Member
    • Oct 2007
    • 2

    How to print 64 bit integer using sprintf() family

    I have a need to print 64 bit integer in Windows and UNIX environments.

    Code Snipet:

    unsigned __int64 mNumRowsRead=12 34567890123456;

    sprintf(buf, "%d row(s) read", mNumRowsRead);
    sprintf(buf, "%s row(s) read", mNumRowsRead);
    sprintf_s(buf, "%d row(s) read", mNumRowsRead);
    itoa(mNumRowsRe ad,buff2,10);
    cout << buff2 << endl;
    sprintf_s(buf, "%s row(s) read", buff2);

    First sprintf converted the 64 bit integer to 32 bits. The out interger is 1015724736 instead of 123456789012345 6.
    Second sprintf throws exception.
    Third sprintf_s behavior like first one.
    Last sprintf_s throws exception without itoa(). With itoa() the number was truncated. itoa() must not be able to handle 64 bit integer as expected. So, do we have 64 bit version of itoa()?? Or how do I print a 64 bit integer as part of a string. Thanks for the help.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    On windows you need to let sprintf know it is a 64 bit integer using the I64 prefix
    [code=c]
    sprintf(buf, "%I64d row(s) read", mNumRowsRead);
    [/code]

    more information here

    Comment

    • jannlin
      New Member
      • Oct 2007
      • 2

      #3
      Originally posted by Banfa
      On windows you need to let sprintf know it is a 64 bit integer using the I64 prefix
      [code=c]
      sprintf(buf, "%I64d row(s) read", mNumRowsRead);
      [/code]

      more information here
      Thanks for the help. I suppose I need to use I64u for unsigned 64 bit integer. Is I64d for signed 64 bit integer? Thanks.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by jannlin
        Thanks for the help. I suppose I need to use I64u for unsigned 64 bit integer. Is I64d for signed 64 bit integer? Thanks.
        That's correct, it's all in the link I provided.

        Comment

        Working...