Thread: how to create and save more than 10^18 Integer Data Type?

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    1

    how to create and save more than 10^18 Integer Data Type?

    hi
    i am new to c programming so.
    unsigned long long data type have max range up to 18446744073709551615
    Code:
    unsigned long long ll = 18446744073709551615;
    how can i create integer data type to save more than 10^18 value ?
    thx
    Last edited by think2021; 01-19-2021 at 07:21 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    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.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,136
    If you don't need more than 128 bits there are native 128-bit values available on 64-bit machines. The maximum for an unsigned 128-bit value is 340282366920938463463374607431768211455, which is about 3.4 * 10^38.
    In gcc and clang the type is called __int128 for signed and unsigned __int128 for unsigned.
    Code:
    #include <stdio.h>
     
    const char *to_str(unsigned __int128 n)
    {
        static char buf[40];
        int i = sizeof buf;
        buf[--i] = '\0';
        for ( ; n >= 10; n /= 10) buf[--i] = '0' + (int)(n % 10);
        buf[--i] = '0' + (int)(n % 10);
        return &buf[i];
    }
     
    unsigned __int128 from_str(const char *s)
    {
        unsigned __int128 n = 0;
        for ( ; *s; ++s) n = (n * 10) + (*s - '0');
        return n;
    }
     
    int main()
    {
        // No 128-bit constants available, so if you want to load it
        // with a value larger than 64 bits you need to use a string.
        const char *value = "123456789012345678901234567890123456789";
     
        // No predefined I/O routines for __int128, either.
        unsigned __int128 n = from_str(value);
        n *= 2;
        printf("%s\n", to_str(n));
     
        return 0;
    }
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create integer type that is always 32 bit
    By MartinR in forum C Programming
    Replies: 11
    Last Post: 06-17-2018, 06:12 AM
  2. how to create an array of enum data type?
    By debugg in forum C++ Programming
    Replies: 9
    Last Post: 09-14-2012, 08:52 AM
  3. create new data type
    By macroasm in forum C Programming
    Replies: 15
    Last Post: 05-18-2009, 05:46 AM
  4. Save integer variable
    By metaTron in forum C Programming
    Replies: 3
    Last Post: 01-11-2006, 11:21 PM
  5. Create data type
    By Malefaust in forum C Programming
    Replies: 2
    Last Post: 09-30-2004, 06:06 AM

Tags for this Thread