counting how long an integer is

This is a discussion on counting how long an integer is within the C Programming forums, part of the General Programming Boards category; Hi there is there anyway of counting how long an integer is, using something like strlen, since I dont think ...

  1. #1
    Unregistered
    Guest

    Talking counting how long an integer is

    Hi there

    is there anyway of counting how long an integer is, using something like strlen, since I dont think that works with integers

    e.g. 100000000

    is 9 characters long

    would appreciate any help

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Convert it to a string and use strlen()

    By the way, the example number you gave was too big to be a regular int. Make it a long int.

    for ints, use itoa(), for longs, ltoa(). The usage is:

    itoa(num, string, 10);

    where the last parameter is always "10" if you are using the base-10 (normal) number system.
    Last edited by Sebastiani; 11-20-2001 at 11:06 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    By the way, the example number you gave was too big to be a regular int.
    That depends on the o/s. 32 bit o/s's will use 32 bit ints, allowing signed values up to 2147483647.
    zen

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's another way. Credit goes to someone who posted this on the old board.
    Code:
    int numlen(int n)
    {
       //get the length of a integer this will work for the length of longs also
       //but this function will not work with floats or doubles because of the decimal point.
       return (log10(n)+1);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting Integer help!
    By CProg3 in forum C Programming
    Replies: 5
    Last Post: 04-25-2009, 07:04 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 05:06 PM
  4. Knight's Tour Recursion Problem
    By thephreak6 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 10:18 AM

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