I know I can do:
n = floor(log10(i)) + 1;
Or I can do a quick loop:
while(i) {
n++;
i/=10;
}
Is there any better way than a complicated math operation, or a loop to achieve the goal? For example: if i = 1234, then n = 4.
I know I can do:
n = floor(log10(i)) + 1;
Or I can do a quick loop:
while(i) {
n++;
i/=10;
}
Is there any better way than a complicated math operation, or a loop to achieve the goal? For example: if i = 1234, then n = 4.
The shortest way I know of (not computationally, just in terms of typing) is to call snprintf(3)
:
int n = snprintf(NULL, 0, "%d", i);
convert it to a string (itoa) and count the number of characters? (might be not the best performance-wise though)