Thread: How to print 11 digit int64_t number with sprintf ?

  1. #1
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43

    How to print 11 digit int64_t number with sprintf ?

    Hello there,

    I want to print a 10 or 11 digit number say 99999999999 with sprintf. For this, I used this code:

    Code:
    gchar*
    convert_value (const gchar *value, gchar type)
    {
        gchar buff[strlen(value)];
        gchar *fstr = malloc (sizeof buff);
        int64_t dvalue; double fvalue;               <== CHECK HERE
        
        switch (type)
        {
            case 'd':
                dvalue = strtol (value, NULL, 0);
                sprintf (buff, "%lld", dvalue);              <== AND HERE
                break;
            case 'f':
                fvalue = strtof (value, NULL);
                sprintf (buff, "%.2lf", fvalue);
                break;
            default:
                strcpy (buff, value);
        }
        
        strcpy (fstr, buff);
        
        return fstr;
    }
    Usage:

    Code:
    convert_value ("99999999999", 'd')
    But, the output is showing me some other 10 digit numbers probably starting from 2**********.
    Please help.
    NB: Format specifier "I64d" is also not working.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If I recall correctly, MinGW GCC on Windows has known printf issues on long long.

    Tim S.

    Related post that might help. Displaying a 'long long' with printf
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Test code that depends on the headers inttypes.h and stdint.h.

    Just to test your compiler.

    Tim S.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    int main()
    {
        uint64_t unum = UINT64_MAX;
        int64_t snum = INT64_MAX;
        /* int64_t snum = 99999999999LL; */
        char buff[256]={0};
    
        sprintf (buff, "%"PRIu64"\n", unum);
        printf("buff:= %s", buff);
    
        sprintf (buff, "%"PRId64"\n", snum);
        printf("buff:= %s", buff);
    
        return 0;
    }
    Last edited by stahta01; 05-31-2012 at 03:08 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    your strtol probably doesn't return 64 bit values. its return value is specified to be 'long int' which apparently in your case is 32 bits.

    per the definition of strtol
    "If the correct value is out of the range of representable values, LONG_MAX or LONG_MIN is returned, and the global variable errno is set to ERANGE"

  5. #5
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Quote Originally Posted by stahta01 View Post
    Test code that depends on the headers inttypes.h and stdint.h.

    Just to test your compiler.

    Tim S.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    int main()
    {
        uint64_t unum = UINT64_MAX;
        int64_t snum = INT64_MAX;
        /* int64_t snum = 99999999999LL; */
        char buff[256]={0};
    
        sprintf (buff, "%"PRIu64"\n", unum);
        printf("buff:= %s", buff);
    
        sprintf (buff, "%"PRId64"\n", snum);
        printf("buff:= %s", buff);
    
        return 0;
    }
    here is the output:

    Code:
    buff:= 18446744073709551615
    buff:= 9223372036854775807

  6. #6
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Ok, I got it to work,
    here is the corrected code. Bye the way thanks stahta01

    Code:
    gchar *
    convert_value (const gchar *value, gchar type)
    {
        gchar buff[256] = {0};
        int64_t dvalue; double fvalue; 
        gchar *final_value;
        final_value = malloc (sizeof buff);
        
        switch (type)
        {
            case 'd':
                dvalue = strtoll (value, NULL, 0);
                sprintf (buff, "%"PRId64"", dvalue);
                break;
            case 'f':
                fvalue = strtof (value, NULL);
                sprintf (buff, "%.2lf", fvalue);
                break;
            default:
                sprintf (final_value, "%s", value);
        }
        
        sprintf (final_value, "%s", buff);
        
        return final_value;
    }
    Actually I was using strtol instead of strtoll.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a number digit by digit.
    By sdfx in forum C Programming
    Replies: 4
    Last Post: 05-10-2011, 05:54 PM
  2. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  5. Number too big for int64_t?
    By Doriän in forum C Programming
    Replies: 23
    Last Post: 09-02-2007, 04:53 PM

Tags for this Thread