# How to calculate length of an integer in C Programming? [closed]

Kindly let me know the easiest way to calculate number of digits in an integer. The scenario is i'll be taking an input from the user which will be a long integer. If the length of the integer is equal to 11 then a message is displayed successful else it shows an error message. Lastly if it gives an error message I would like to break the program and no other instruction should be shown on the screen.

I have tried the code below doing it so far with while and if statements, program does runs but not gives the output that is required.

``````printf("Enter your Account Number :\n");
scanf("%ld", &accno);

while(accno != 0)
{
++count;
}
count = count;
if (count == 11){
printf("You are existing customer of bank.");
}else{
printf("Sorry you are not a customer of our bank");
}
``````

## closed as unclear what you're asking by gnat, Jean-François Fabre♦Apr 11 at 18:58

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• `while(accno != 0) { ++count; }` is an infinite loop – Jean-François Fabre Apr 11 at 14:27
• `count = count;`..what is this? – Sourav Ghosh Apr 11 at 14:29
• `int i=0; while((n/=10)>0) i++;` something like this – Banzay Apr 11 at 14:30
• @SouravGhosh It's a well-known technique to guard against the effect of Cosmic Rays ... if you get the timing right, you take the old, correct value (`count`) then assign it back to the original variable just after a cosmic raw has altered the value :-) – TripeHound Apr 11 at 14:34
• @EugeneSh. You choose whether to optimise for speed (`-O3`), size (`-Os`) or cosmic ray protection (the in-development `-Ocrp`). – TripeHound Apr 11 at 14:42

Method 1: Math

There's actually a mathematically smart way to find the number of digits in any number, using the `log` function (base 10):

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

int numlen(unsigned long long n){
if(n==0) return 1;
return (int)(ceil(log10((double)(n)))); //sorry for the multiple castings, as ceil/log both take in and return doubles
}
``````

This method is of course slower, but it's mathematically beautiful.

EDIT: I fully agree with @chux comment that this method, due to the castings and floating point precision problems, is not the most preferable for integer solutions. However, it is still the most mathematically appropriate way of solving the problem, imo.

Method 2: Loops

``````int numlen(unsigned long long n){
int len = 0;

if(n<10) return 1;
while (n) {
n /= 10;
len++;
}
}
``````

Method 3: Recursion

``````int isElevenDigits(unsigned long long n){
• The problem is that `log10` takes in a `double` by default, so sometimes it'll throw an error if you don't do a force casting – Samleo Apr 12 at 3:07
• Should you care for other details:`unsigned long long n; ... (double)(n /* not x */)` --> the cast to `double` incurs rounding for large `n` as `n` is 64-bit and `double` typically past 54 bits rounds. So `9,999,999,999,999,999,999` is exact as `unsigned long long` yet when converted to `double` becomes exactly 1.0e+19 and then is off by 1 for this task. Using FP math for an integer problem tends to run into a host of unsuspecting corner cases and is best avoided for integer problems. – chux Apr 12 at 3:20