Skip to content

Instantly share code, notes, and snippets.

@ButchDean
Last active December 25, 2015 20:19
Show Gist options
  • Save ButchDean/7034499 to your computer and use it in GitHub Desktop.
Save ButchDean/7034499 to your computer and use it in GitHub Desktop.
Non-iterative approach to finding the number of digits in an integer. Also works for both positive and negative numbers.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
int digits;
digits = (atoi(argv[1]) == 0) ? 1 : (int)log10(abs(atoi(argv[1]))) + 1;
printf("Number of digits in %s is %d\n", argv[1], digits);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment