how to find the amount of characters in unsigned long long

7.4k views Asked by At

hi I have 2 question the first one is the one in the title and the other one is here: does unsigned long long is the biggest integer (can hold the biggest amount of characters)? cause I need an int that can hold few millions characters (digits) is this possible? I'm coding in C. and this is connecting me to the other question how can I display the amount of digits on the screen? is it need to be like this?:

printf("%d", intName.length)

thanks every one!!

4

There are 4 answers

1
Tharanga Abeyseela On
printf("%llu", xxxxxxxxx);

 the ll (el-el) long-long modifier with the u (unsigned) conversion

you can also use

 uint64_t a;
 uint32_t b;

But you need to inlcude inttypes.h library that gives you types such as int32_t, int64_t, uint64_t.

7
norlesh On

I am assuming that when you refer to the amount of characters you mean the number of digits in the number. If so then one this question has everything you need to know and included code similar to this

int numberOfDigits(unsigned long long n) 
{
    if (n == 0)
        return 0;
    return floor( log10( abs( n ) ) ) + 1;
}

as for holding a few million digits you probably want to look into using a library such as The GNU Multiple Precision Arithmetic Library which includes the function

size_t mpz_sizeinbase( const mpz_t op, int base )

which will tell you how many digits your number has.

3
Frank On

C99 provides intmax_t (and uintmax_t) which will be the largest supported integer type (typically 64 bit.)

Assuming you have a conforming C99 snprintf then you can get the number of digits with:

length = snprintf(NULL, 0, "%llu", value);

for an unsigned long long value (and %ju for uintmax_t.)

Otherwise you'll have to pass a buffer in (yuk) or do something manually like:

length = value < 10 ? 1 :
         value < 100 ? 2 :
         ...

also yuk!

But this is all pretty irrelevant if you really do want million-digit integers, in which case you'll need to use a library such as gmp to work with such big numbers.

0
Loic On

The maximal decimal length of an unsigned integer of a given type of bit length bitlen is given by 1 + floor(log10(2^bitlen-1)) (mathematically, without taking overflows and rounding errors into account). The approximation 1/log2(10) ~ 4004.0/13301 (obtained with continued fractions, see http://en.wikipedia.org/wiki/Continued_fraction) leads to the formula 1 + bitlen * 4004 / 13301 (computationally, i.e. the division rounds down). Mathematical details are given in the comments of the snippet below.

#include <limits.h>
#include <stdio.h>

/**
 * Maximal number of digits in the decimal representation of an unsigned type.
 *
 * floor( log2(2^bitlen - 1) / log2(10) ) == floor( bitlen / log2(10) )
 * otherwise an integer n would exist with
 *     log2(2^bitlen - 1) / log2(10) < n < bitlen / log2(10)
 *     log2(2^bitlen - 1) < n * log2(10) < bitlen
 *     2^bitlen - 1 < 2^(n * log2(10)) < 2^bitlen
 *     2^bitlen - 1 < (2^log2(10))^n < 2^bitlen
 *     2^bitlen - 1 < 10^n < 2^bitlen
 *     which is impossible
 *
 * 1 / log2(10) ~ 0.301029995663981
 * 4004 / 13301 ~ 0.30102999774453
 *
 *     1 + floor( log10(2^bitlen - 1) )
 *  == 1 + floor( log2(2^bitlen - 1) / log2(10) )
 *  == 1 + floor( bitlen / log2(10) )
 *  <= 1 + floor( bitlen * 4004.0 / 13301 )
 *  == 1 + bitlen * 4004 / 13301
 * with equality for bitlen <= 13300 == 8 * 1662.5
 */
#define DECLEN(unsigned_t) (1 + CHAR_BIT*sizeof(unsigned_t) * 4004 / 13301)

int main(int argc, char *argv[]) {
    printf("unsigned char      : %zu\n", DECLEN(unsigned char));
    printf("short unsigned     : %zu\n", DECLEN(short unsigned));
    printf("unsigned           : %zu\n", DECLEN(unsigned));
    printf("long unsigned      : %zu\n", DECLEN(long unsigned));
    printf("long long unsigned : %zu\n", DECLEN(long long unsigned));
    return 0;
}