is there a function that counts the digits of an int

This is a discussion on is there a function that counts the digits of an int within the C Programming forums, part of the General Programming Boards category; Just curious because it would make printing out an matrix much easier. Thanks....

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    is there a function that counts the digits of an int

    Just curious because it would make printing out an matrix much easier.

    Thanks.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <math.h>
    ...
    (int)log10( n ) + 1;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    The obvious way is to sprintf() it to a temp buffer and then use strlen
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    is the (int) necessary in front of the log(n)?

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    yes it should be... I think you could have problems using log10 because of rounding errors.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Just curious because it would make printing out an matrix much easier.

    There is also this macro I found on another list.
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    #define DECIMALS(itype) \
    (sizeof(itype) * (CHAR_BIT * 12655UL) / 42039 \
    + ((itype)-1 < 0) + 1)
    
    int main(void)
    {
       int matrix[][5] = 
       {
          { INT_MIN, INT_MIN + 1, INT_MIN + 2, -1, 0 },
          { 0, 1, INT_MAX - 2, INT_MAX - 1, INT_MAX },
       };
       size_t i,j;
       for(i = 0; i < sizeof matrix / sizeof *matrix; ++i)
       {
          for(j = 0; j < sizeof *matrix / sizeof **matrix; ++j)
          {
             printf("%*d ", DECIMALS(int), matrix[i][j]);
          }
          putchar('\n');
       }
       return 0;
    }
    
    /* my output
    -2147483648 -2147483647 -2147483646          -1           0 
              0           1  2147483645  2147483646  2147483647 
    */
    This performs (an approximation of) the logarithmic calculation at compile time.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Speaking of macros...
    Code:
    #define ULEN32(x) ( \
        (x)<10?1: \
        (x)<100?2: \
        (x)<1000?3: \
        (x)<10000?4: \
        (x)<100000?5: \
        (x)<1000000?6: \
        (x)<10000000?7: \
        (x)<100000000?8: \
        (x)<1000000000?9: 10 )
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Many suggestions so far. Oh well, here's another:
    Code:
    int GetNrOfDigits(int Number)
    {
       int Digits = 0;
       while(Number > 0)
       {
          Digits++;
          Number /= 10;
       }
       return Digits;
    }
    (Only works for positive integers, and 0 comes out as 0 digits though, you could use a special case if this is unwanted)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Code:
    int GetNrOfDigits(int Number)
    {
       int Digits = 0;
       do {
          Digits++;
          Number /= 10;
       } while(Number > 0);
       return Digits;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21