I would like to know how I can find the length of an integer in C.
For instance:
and so on.
How can I do this in C?
How to Find Length of Array in C. C Program, C Programming / Leave a Comment. How to Find the Length of the Array in C | To find the length of the array we have to use sizeof () function. The sizeof () function calculates the size in bytes of the passed variable or data type. To calculate the length of the array, first, ...
The short answer is to use the myInt.ToString ().Length method and replace myInt with your integer variable. You can also use the loop to get the count of integer variable value in C#.
Converting to a string and finding the length property? int num = 123 string strNum = to_string (num); // 123 becomes "123" int length = strNum.length (); // length = 3 char array [3]; // or whatever you want to do with the length Not very efficient though. int digits ( int x ) { return ( (bool) x * (int) log10 ( abs ( x ) ) + 1 ); }
The number of digits of an integer x is equal to 1 + log10 (x). So you can do this: Or you can run a loop to count the digits yourself: do integer division by 10 until the number is 0:
Why not just 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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With