92

I would like to know how I can find the length of an integer in C.

For instance:

  • 1 => 1
  • 25 => 2
  • 12512 => 5
  • 0 => 1

and so on.

How can I do this in C?

APerson
  • 8,140
  • 8
  • 35
  • 49
marabunta2048
  • 1,071
  • 1
  • 8
  • 9
  • 1
    What's the definition of "length" if the integer is 0? Negative? – kennytm Jun 18 '10 at 09:11
  • 1
    See http://stackoverflow.com/questions/679602/fastest-way-to-calculate-the-decimal-length-of-an-integer-net. It's almost a duplicate, but not exact as it's a .NET question. – ChrisF Jun 18 '10 at 09:36
  • 7
    the right question is not the length of the integer, but which is the minimal number of decimal digits needed to represent that number (held into a C int). log10 is your friend: log10(10000) = 4, +1 the number of digits (log10 must be truncated)... if the num is neg, you need one more for the - symbol, if you want to count it, and log10(-num) (since log of a neg number is "problematic". – ShinTakezou Jun 18 '10 at 09:41
  • How sure are you that `log10(10000)` will return 4 and not 3.999999...? – Keith Thompson Aug 29 '11 at 05:58
  • 2
    Hmm. Experiment shows that `log10(10**n)` yields the exact value for powers of 10 from 1 to 2**19, at least with gcc and glibc. But I wouldn't count on it for all implementations. (`**` denotes exponentiation; there's no such operator in C.) – Keith Thompson Aug 29 '11 at 06:02
  • Duplicate: http://stackoverflow.com/questions/1068849/how-do-i-determine-the-number-of-digits-of-an-integer-in-c – A Person Jan 10 '14 at 08:11
  • @APerson Whoa! I just flagged this same question as a duplicate. You must be a person, too. – APerson Nov 01 '14 at 20:54
  • For a second I almost thought I ended up on Code Golf instead of SO – sportzpikachu Mar 20 '20 at 08:44

30 Answers30

131

C:

You could take the base-10 log of the absolute value of the number, round it down, and add one. This works for positive and negative numbers that aren't 0, and avoids having to use any string conversion functions.

The log10, abs, and floor functions are provided by math.h. For example:

int nDigits = floor(log10(abs(the_integer))) + 1;

You should wrap this in a clause ensuring that the_integer != 0, since log10(0) returns -HUGE_VAL according to man 3 log.

Additionally, you may want to add one to the final result if the input is negative, if you're interested in the length of the number including its negative sign.

Java:

int nDigits = Math.floor(Math.log10(Math.abs(the_integer))) + 1;

N.B. The floating-point nature of the calculations involved in this method may cause it to be slower than a more direct approach. See the comments for Kangkan's answer for some discussion of efficiency.

Jordan Lewis
  • 16,900
  • 4
  • 29
  • 46
  • 2
    Actually you should use floor and add 1 instead. Math.Ceil(Math.Log(99)) = 2 but Math.Ceil(Math.Log(10)) = 1. Math.Floor(Math.Log(99)) + 1 = 2 and Math.Floor(Math.Log(10)) = 2 – Sani Huttunen Jun 18 '10 at 09:20
  • The question isn't entirely clear on the definition of length (so you could possibly have thought 'number of digits excluding leading zeros'), but I would expect 0 and -1 to return 1 and 2 as the length of their character representation rather than -2147483648 and 1. – Pete Kirkham Jun 18 '10 at 09:40
  • @Pete Thanks for reminding me about log's domain limitation and the negative number case - I've edited my answer. – Jordan Lewis Jun 18 '10 at 09:51
  • 1
    +1 nice & short - this is my preferred answer, even if it's the slowest - after all, the speed difference isn't huge and this kind of code is very unlikely to be a perf. bottleneck anyhow. – Eamon Nerbonne Jun 18 '10 at 13:20
  • +1 for the Note (and following comments) about efficiency. Every programmer should know basic math indeed, but should also know sometimes math isn't the faster route thanks to friggin' fast binary processors. Talking about C, this can easily become a bottleneck. – cregox Feb 05 '14 at 20:29
  • 3
    This does not work for 999999999999999999, a C integer that will be converted to a greater double value, and thus produce an erroneous count of digits. The OP did not specify `int`, just integer. – chqrlie Nov 23 '15 at 23:08
  • Addressing the `== 0` case, here is a one-liner: `(the_integer == 0 ? 1 : (int)floor(log10(abs(the_integer))) + 1)` – luckydonald Nov 22 '16 at 14:16
  • Sweet! For a **large number** I modified like so: `long long nDigits = floor(log10(llabs(the_integer))) + 1;` – protoEvangelion Mar 27 '17 at 19:41
  • abs is provided by stdlib, not by math – Ace.C Apr 20 '20 at 19:58
62

If you're interested in a fast and very simple solution, the following might be quickest (this depends on the probability distribution of the numbers in question):

int lenHelper(unsigned x) {
    if (x >= 1000000000) return 10;
    if (x >= 100000000)  return 9;
    if (x >= 10000000)   return 8;
    if (x >= 1000000)    return 7;
    if (x >= 100000)     return 6;
    if (x >= 10000)      return 5;
    if (x >= 1000)       return 4;
    if (x >= 100)        return 3;
    if (x >= 10)         return 2;
    return 1;
}

int printLen(int x) {
    return x < 0 ? lenHelper(-x) + 1 : lenHelper(x);
}

While it might not win prizes for the most ingenious solution, it's trivial to understand and also trivial to execute - so it's fast.

On a Q6600 using MSC I benchmarked this with the following loop:

int res = 0;
for(int i = -2000000000; i < 2000000000; i += 200) res += printLen(i);

This solution takes 0.062s, the second-fastest solution by Pete Kirkham using a smart-logarithm approach takes 0.115s - almost twice as long. However, for numbers around 10000 and below, the smart-log is faster.

At the expense of some clarity, you can more reliably beat smart-log (at least, on a Q6600):

int lenHelper(unsigned x) { 
    // this is either a fun exercise in optimization 
    // or it's extremely premature optimization.
    if(x >= 100000) {
        if(x >= 10000000) {
            if(x >= 1000000000) return 10;
            if(x >= 100000000) return 9;
            return 8;
        }
        if(x >= 1000000) return 7;
        return 6;
    } else {
        if(x >= 1000) {
            if(x >= 10000) return 5;
            return 4;
        } else {
            if(x >= 100) return 3;
            if(x >= 10) return 2;
            return 1;
        }
    }
}

This solution is still 0.062s on large numbers, and degrades to around 0.09s for smaller numbers - faster in both cases than the smart-log approach. (gcc makes faster code; 0.052 for this solution and 0.09s for the smart-log approach).

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
  • 13
    I shudder to think what the second version would look like written entirely with the ternary operator... – Eamon Nerbonne Jun 18 '10 at 13:22
  • If this was used to munch through long lists of numbers the amount of branching code would cause havoc with the CPU`s branch prediction and not produce the fastest execution I`m afraid. – Lloyd Crawley Oct 23 '13 at 08:52
  • 1
    In my benchmark it's still the fastest solution - note that all other integral solutions also require several branches, and the only real alternative is an int-to-double conversion with a floating point log (which as it turns out isn't cheap either). – Eamon Nerbonne Oct 23 '13 at 16:15
  • 1
    @earthdan: that solution is fine, but quite slow due to the divisions. It also always uses more branches that the second version of this code, and more on average than the first solution posted here. Also, that solution is quite clever (in a bad way) in that the reason it works isn't entirely obvious. If you want a short+obvious solution, use http://stackoverflow.com/a/3068412/42921; if you want a fast and obvious solution use this. Can't imagine the use case for http://stackoverflow.com/a/6655759/2382629 (though it's of course a fun intellectual exercise!) – Eamon Nerbonne Mar 17 '14 at 12:55
40
int get_int_len (int value){
  int l=1;
  while(value>9){ l++; value/=10; }
  return l;
}

and second one will work for negative numbers too:

int get_int_len_with_negative_too (int value){
  int l=!value;
  while(value){ l++; value/=10; }
  return l;
}
zed_0xff
  • 32,417
  • 7
  • 53
  • 72
21

You can write a function like this:

unsigned numDigits(const unsigned n) {
    if (n < 10) return 1;
    return 1 + numDigits(n / 10);
}
Kangkan
  • 15,267
  • 10
  • 70
  • 113
  • 4
    This is unnecessarily inefficient - why use up to around 18 recursive function calls when you can do it with one call to log10? – Jordan Lewis Jun 18 '10 at 09:27
  • but making it not recursive could be in some circumstances, depending on the cpus and availability of float coprocessor, faster than using a fuction like log10 – ShinTakezou Jun 18 '10 at 09:44
  • 12
    @Jordan Lewis calling this 20 million times takes 1.8s on my netbook; calling your code takes 6.6 seconds (gcc -O3 for both). One call to log10 is very much slower than all the recursive calls this makes. – Pete Kirkham Jun 18 '10 at 09:46
  • 2
    @Pete I see .001s for the log10 version and .44s for the recursive version on my Intel i7, for 20 million times with -O3. – Jordan Lewis Jun 18 '10 at 09:59
  • @Jordan are you calling it with the same or different values? ` int c = 0; for ( int i = -10000000; i < 10000000; ++i ) c += printed_length(i); printf ( "total %d\n", c); ` – Pete Kirkham Jun 18 '10 at 10:08
  • @Pete I was iterating from i = 0 to 2 million, and using i as the parameter to printed_length as well. I will try again with your parameters, including the +=. – Jordan Lewis Jun 18 '10 at 10:22
  • Well, surely you weren't using this numDigits implementation for the negative values, as it doesn't handle negative values? – Jordan Lewis Jun 18 '10 at 10:25
  • @Pete and @Jordan thanks for bringing in the context of efficiency. I proposed my answer looking at the simplicity. – Kangkan Jun 18 '10 at 10:46
  • 1
    @Jordan I added `if ( x < 0 ) return 1 + printed_length ( -x );` to the start of it – Pete Kirkham Jun 18 '10 at 10:46
  • 6
    @Pete I must have made a mistake causing the compiler to optimize out the actual calls before. I now observe a gap like yours - .26 seconds for the recursive version, and 1.68 seconds for the floating point arithmetic version. Interesting! – Jordan Lewis Jun 18 '10 at 11:07
  • Quite possibly the recursive version is less recursive that it looks once compiled - depending on version/options, gcc will partially unroll short recursive calls like this; that's particularly easy here since it's a tail call. – Eamon Nerbonne Jun 18 '10 at 11:33
  • @Eamon Nerbonne the `1 + ` part stops it being a trivial tail call. – Pete Kirkham Jun 18 '10 at 15:42
  • @Pete: indeed; not trivial; still inlinable though; and with a maximum depth of a dozen or so inlining would work well. – Eamon Nerbonne Jun 18 '10 at 17:18
  • gcc unrolls it (at least gcc 4.8 does) 16 instructions with -O2, so it's really the best solution, even better than log and better then the len helper which has a lot of compares (keyword: branch prediction) – hellow May 24 '13 at 10:16
  • @Jordan Lewis looks like log()/floor nor logl()/floorl() will work with long integer values. here a test: minimum signed long integer on this system is: -9223372036854775808 maximum signed long integer on this system is: 9223372036854775807 get_num_len2() i=-9223372036854775808 len=-2147483648 get_num_len2() i=-9223372036854775807 len=2 – 0x0C4 Nov 01 '16 at 14:10
12

length of n:

length =  ( i==0 ) ? 1 : (int)log10(n)+1;
Fritz G. Mehner
  • 16,550
  • 2
  • 34
  • 41
  • You should probably avoid rounding via casting and instead go or a more explicit (a.k.a. portable and consistent) rounding approach. – Chris Lutz Jun 18 '10 at 09:18
  • how is `casting` implemented? how a function like floor can be implemented? (we are assuming a processor with ieee in hardware, or through a math coprocessor, or the availability of software function to perform the same function normally present on fp-capable processors)... at the end (int) is portable and consistent in most cases (I dare say, all the cases we normally care of) – ShinTakezou Jun 18 '10 at 09:48
  • As mentioned in other posts, this will fail when n = 0 – Jamie Wong Jun 18 '10 at 09:59
  • @Lutz: what kind of portability are you buying by assuming casting from double to int is undefined? Is there actually a relevant platform where this is the case? – Eamon Nerbonne Jun 18 '10 at 13:34
  • @Chris Lutz If it's a standards compliant C implementation, then it obeys *When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero).* – Pete Kirkham Jun 18 '10 at 17:44
  • @Pete, @Eamon - My bad. I thought it was undefined or implementation defined. – Chris Lutz Jun 18 '10 at 23:41
8

The number of digits of an integer x is equal to 1 + log10(x). So you can do this:

#include <math.h>
#include <stdio.h>

int main()
{
    int x;
    scanf("%d", &x);
    printf("x has %d digits\n", 1 + (int)log10(x));
}

Or you can run a loop to count the digits yourself: do integer division by 10 until the number is 0:

int numDigits = 0;
do
{
    ++numDigits;
    x = x / 10;
} while ( x );

You have to be a bit careful to return 1 if the integer is 0 in the first solution and you might also want to treat negative integers (work with -x if x < 0).

IVlad
  • 43,099
  • 13
  • 111
  • 179
8

A correct snprintf implementation:

int count = snprintf(NULL, 0, "%i", x);
sam hocevar
  • 11,853
  • 5
  • 49
  • 68
7

The most efficient way could possibly be to use a fast logarithm based approach, similar to those used to determine the highest bit set in an integer.

size_t printed_length ( int32_t x )
{
    size_t count = x < 0 ? 2 : 1;

    if ( x < 0 ) x = -x;

    if ( x >= 100000000 ) {
        count += 8;
        x /= 100000000;
    }

    if ( x >= 10000 ) {
        count += 4;
        x /= 10000;
    }

    if ( x >= 100 ) {
        count += 2;
        x /= 100;
    }

    if ( x >= 10 )
        ++count;

    return count;
}

This (possibly premature) optimisation takes 0.65s for 20 million calls on my netbook; iterative division like zed_0xff has takes 1.6s, recursive division like Kangkan takes 1.8s, and using floating point functions (Jordan Lewis' code) takes a whopping 6.6s. Using snprintf takes 11.5s, but will give you the size that snprintf requires for any format, not just integers. Jordan reports that the ordering of the timings are not maintained on his processor, which does floating point faster than mine.

The easiest is probably to ask snprintf for the printed length:

#include <stdio.h>

size_t printed_length ( int x )
{
    return snprintf ( NULL, 0, "%d", x );
}

int main ()
{
    int x[] = { 1, 25, 12512, 0, -15 };

    for ( int i = 0; i < sizeof ( x ) / sizeof ( x[0] ); ++i )
        printf ( "%d -> %d\n", x[i], printed_length ( x[i] ) );

    return 0;
}
Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • 1
    If you're going to use `snprintf()`, why not `snprintf(NULL, 0, "%d", x)` and not write anything? (At the very least, use a static buffer in your function.) – Chris Lutz Jun 18 '10 at 09:20
  • 1
    because I haven't had enough coffee yet this morning, and was thinking about the first leg of the answer. – Pete Kirkham Jun 18 '10 at 09:26
  • Should split your replies in two posts, I'd +1 the first one but not the 2nd. – Blindy Jun 18 '10 at 09:30
  • 2
    It rather depends why you want the length - if you want to know how many chars snprintf will require, you're better off using snprintf; if you want silly optimised code, you might want the first one. – Pete Kirkham Jun 18 '10 at 09:49
  • 1
    " C99 allows str to be NULL in this case [the case of n==0], and gives the return value (as always) as the number of characters that would have been written in case the output string has been large enough" so it is ok, why not – ShinTakezou Jun 18 '10 at 10:00
  • (Ah... I suppose the answer was modified, following Lutz's suggestion... ?) – ShinTakezou Jun 18 '10 at 10:02
  • 1
    Your solution does not work for `INT_MIN`. Use a local `unsigned` variable for the tests and initialize it with `x >= 0 ? x : -(unsigned)x` – chqrlie Nov 23 '15 at 23:16
6

Yes, using sprintf.

int num;
scanf("%d",&num);
char testing[100];
sprintf(testing,"%d",num);
int length = strlen(testing);

Alternatively, you can do this mathematically using the log10 function.

int num;
scanf("%d",&num);
int length;
if (num == 0) {
  length = 1;
} else {    
  length = log10(fabs(num)) + 1;
  if (num < 0) length++;
}
Jamie Wong
  • 18,104
  • 8
  • 63
  • 81
  • 1
    No, that's actually rather dangerous and prone to errors. You should use `snprintf()` so you don't have to write (and risk overflowing) anything. – Chris Lutz Jun 18 '10 at 09:11
  • Assuming he was referring to a C integer (not a bignum type) there won't be any issues with overflows. – Jamie Wong Jun 18 '10 at 09:15
  • 1
    There will be issues when computers get bigger. Sure, you'll need a 512 bit computer to break your code, but they'll probably make one someday. – Chris Lutz Jun 18 '10 at 09:17
  • no, likely 128 bit will be the last frontier... there won't be any reason to go beyond (exagerating, I say there's no reason for going beyond 64 bits, but I am _almost_ already wrong, but currently no real 128bit processor are available yet, and they hardly will be, at least in consumer computing... I hope, since the they will need them, it would mean O.S. will be too fat and we'll remember these days as better days) – ShinTakezou Jun 18 '10 at 09:54
  • 1
    you can simply use the return value of `sprintf()` without calling `strlen()` – phuclv Mar 14 '15 at 19:28
5
int digits=1;

while (x>=10){
    x/=10;
    digits++;
}
return digits;
eebbesen
  • 5,070
  • 8
  • 48
  • 70
Pasi
  • 51
  • 1
  • 1
3
sprintf(s, "%d", n);
length_of_int = strlen(s);
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
2

You may use this -

(data_type)log10(variable_name)+1

ex:

len = (int)log10(number)+1;

2

In this problem , i've used some arithmetic solution . Thanks :)

int main(void)
{
    int n, x = 10, i = 1;
    scanf("%d", &n);
    while(n / x > 0)
    {
        x*=10;
        i++;
    }
    printf("the number contains %d digits\n", i);

    return 0;
}
bwass31
  • 65
  • 1
  • 10
1

Quite simple

int main() {
    int num = 123;
    char buf[50];

    // convert 123 to string [buf]
    itoa(num, buf, 10);

    // print our string
    printf("%s\n", strlen (buf));

    return 0;
}
zed_0xff
  • 32,417
  • 7
  • 53
  • 72
Mauro
  • 2,032
  • 3
  • 25
  • 47
1

keep dividing by ten until you get zero, then just output the number of divisions.

int intLen(int x)
{
  if(!x) return 1;
  int i;
  for(i=0; x!=0; ++i)
  {
    x /= 10;
  }
  return i;
}
Graphics Noob
  • 9,790
  • 11
  • 46
  • 44
1

This goes for both negative and positive intigers

    int get_len(int n)
    {
        if(n == 0)
        return 1;

        if(n < 0)    
        {
           n = n * (-1); // if negative
        }

        return  log10(n) + 1;
    }

Same logic goes for loop

  int get_len(int n)
  {
       if(n == 0)
       return 1;

       int len = 0;
       if(n < 0)
       n = n * (-1);

       while(n > 1)
       {
          n /= 10;
          len++;
       }

       return len;
  }
Djordje
  • 437
  • 1
  • 12
  • 24
1

Why don't you cast your integer to String and get length like this :

int data = 123;
int data_len = String(data).length();
manumazu
  • 783
  • 5
  • 3
1

For simple programs...

int num = 456, length=0 // or read value from the user to num
while(num>0){
    num=num/10;
    length++;
}

Use another variable to retain the initial num value.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Vyshnav
  • 11
  • 2
1

Hmm, maybe like this...?

#define _LEN(x) (sizeof(#x)/sizeof(char)-1)
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
FanCo
  • 11
  • 1
  • This will work only and only if the constant number (ex. 123) is given to macro. If it is arithmetic or int variable, this macro will fail – Miradil Zeynalli May 16 '23 at 10:47
0

In my opinion the shortest and easiest solution would be:

int length , n;

printf("Enter a number: ");

scanf("%d", &n);

length = 0;

while (n > 0) {
   n = n / 10;
   length++;
}

printf("Length of the number: %d", length);
A Person
  • 1,350
  • 12
  • 23
Flo
  • 1
  • If (n) equals 0, this will return length of (n) as 0. Use `do...while` so loop is executed once when (n) equals 0. – Kevin Fegan Jan 11 '14 at 02:18
0

My way:

Divide as long as number is no more divisible by 10:

u8 NumberOfDigits(u32 number)
{
    u8 i = 1;
    while (number /= 10) i++;

    return i;
}

I don't know how fast is it in compared with other propositions..

tBlabs
  • 1
0
int intlen(int integer){
    int a;
    for(a = 1; integer /= 10; a++);
    return a;
}
Ruza
  • 11
  • 2
0

A more verbose way would be to use this function.

int length(int n)
{
    bool stop;
    int nDigits = 0;
    int dividend = 1;
    do
    {
        stop = false;
        if (n > dividend)
        {
            nDigits = nDigits + 1;
            dividend = dividend * 10;
        }
        else {
            stop = true;
        }


    }
    while (stop == false);
    return nDigits;
}
0
int returnIntLength(int value){
    int counter = 0;
    if(value < 0)
    {
        counter++;
        value = -value;
    }
    else if(value == 0)
        return 1;

    while(value > 0){
        value /= 10;
        counter++;
    }

    return counter;
}

I think this method is well suited for this task:

value and answers:

  • -50 -> 3 //it will count - as one character as well if you dont want to count minus then remove counter++ from 5th line.

  • 566666 -> 6

  • 0 -> 1

  • 505 -> 3

0

Solution

Use the limit where the integer length changes, in the case of the decimal it is a power of 10, and thus use a counter for each verification that the specified integer has not exceeded the limit.

With the math.h dependency:

#include <math.h>

int count_digits_of_integer(unsigned int integer) {
    int count = 1;

    while(1) {
        int limit = pow(10, count);
        if(integer < limit) break;
        count++;

    }

    return count;
}

Without dependency:

int int_pow(int base, int exponent) {
    int potency = base;

    for(int i = 1; i < exponent; i++) potency *= base;

    return potency;

}

int count_digits_of_integer(unsigned int integer) {
    int count = 1;
    
    while(1) {
        int limit = int_pow(10, count);
        if(integer < limit) break;
        count++;

    }
    
    return count;
}

Implementation

#include <stdio.h>

// Copy and paste the solution code here

int main() {
    
    printf("%i -> (%i digits)\n", 0, count_digits_of_integer(0));
    printf("%i -> (%i digits)\n", 12, count_digits_of_integer(12));
    printf("%i -> (%i digits)\n", 34569, count_digits_of_integer(34569));
    printf("%i -> (%i digits)\n", 1234, count_digits_of_integer(1234));
    printf("%i -> (%i digits)\n", 3980000, count_digits_of_integer(3980000));
    printf("%i -> (%i digits)\n", 100, count_digits_of_integer(100));
    printf("%i -> (%i digits)\n", 9, count_digits_of_integer(9));
    printf("%i -> (%i digits)\n", 385784, count_digits_of_integer(385784));
    
    return 0;
}

Output:

0 -> (1 digits)
12 -> (2 digits)
34569 -> (5 digits)
1234 -> (4 digits)
3980000 -> (7 digits)
100 -> (3 digits)
9 -> (1 digits)
385784 -> (6 digits)
David Gaspar
  • 407
  • 5
  • 12
0

Similar to all answers here, if you are already using string.h, you can do the sprintf right at macro, instead of having separate function:

#define DIGIT_COUNT(x) (snprintf(NULL, 0, "%d", (x)))

This macro will count negative sign towards digit. If you don't want that, you can simply use abs function:

#define DIGIT_COUNT(x) (snprintf(NULL, 0, "%d", abs((x))))
Miradil Zeynalli
  • 423
  • 4
  • 18
-1

I think I got the most efficient way to find the length of an integer its a very simple and elegant way here it is:

int PEMath::LengthOfNum(int Num)
{
int count = 1;  //count starts at one because its the minumum amount of digits posible
if (Num < 0)
{
    Num *= (-1);
}

for(int i = 10; i <= Num; i*=10)
{
     count++;
}      
return count;
                // this loop will loop until the number "i" is bigger then "Num"
                // if "i" is less then "Num" multiply "i" by 10 and increase count
                // when the loop ends the number of count is the length of "Num".
}
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
shiups
  • 1
  • 1
    `Num *= (-1)` seems a bit heavy handed, although your compiler will probably optimize it to the much more efficient `Num = -Num`. Note that multiplication is often an expensive operation whereas negating an integer is pretty trivial. – Arunas Oct 09 '13 at 22:46
  • 1
    Not C code. Does not work for INT_MIN, does not work for INT_MAX either, actually does not work for numbers larger or equal to the largest power of 10 less than INT_MAX, such as 1000000000 for `int32_t`. – chqrlie Nov 23 '15 at 23:26
-1

int main(void){ unsigned int n, size=0;

printf("get the int:");
scanf("%u",&n);

/*the magic*/
for(int i = 1; n >= i; i*=10){
    size++;
}

printf("the value is: %u \n", n);
printf("the size is: %u \n", size);

return 0;

}

p1r0
  • 418
  • 6
  • 10
-1

You can also use this function to find the length of an integer:

int countlength(int number)
{
    static int count = 0;
    if (number > 0)
    {
        count++;
        number /= 10;
        countlength(number);
    }
    return count;
}
M.Jafar
  • 17
  • 5
  • 1
    This function will only work *once*. On a second 'top level' call, the `count` will not get reset to zero. – Adrian Mole Sep 24 '22 at 18:50
  • static inside of the function keeps the value for future function calls. For this type of recursion it seems it is needed, but as Adrian said, it will work only once, – Miradil Zeynalli May 16 '23 at 10:45
-2
#include  <stdio.h>
int main(void){
    int c = 12388884;
    printf("length of integer is: %d",printf("%d",c));
    return 0;
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Nasir Shah
  • 5,033
  • 1
  • 11
  • 11