size of long

Discussion in 'C Programming' started by David Hill, Aug 14, 2003.

  1. David Hill

    David Hill Guest

    Hello -
    What value should SIZE have?

    int
    main(void)
    {
    long num = 1343331L;
    char buf[SIZE];

    snprintf(buf, sizeof(buf), "%s", num);
    printf("%s\n", buf);

    return (0);
    }
    David Hill, Aug 14, 2003
    #1
    1. Advertising

  2. David Hill

    Lew Pitcher Guest

    David Hill wrote:
    > Hello -
    > What value should SIZE have?


    A size_t value greater than 0.

    The standard doesnt say much about the /size/ of a long integer. It mostly
    talks about the /range of values/ that a long int must be able to represent.
    It is entirely concievable that a long int has the same size as a char on
    some implementations (them's /big/ characters ;-) ).

    > int
    > main(void)
    > {
    > long num = 1343331L;
    > char buf[SIZE];
    >
    > snprintf(buf, sizeof(buf), "%s", num);
    > printf("%s\n", buf);
    >
    > return (0);
    > }



    --

    Lew Pitcher, IT Consultant, Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    Lew Pitcher, Aug 14, 2003
    #2
    1. Advertising

  3. David Hill

    -berlin.de Guest

    David Hill <> wrote:
    > What value should SIZE have?


    > int
    > main(void)
    > {
    > long num = 1343331L;
    > char buf[SIZE];


    > snprintf(buf, sizeof(buf), "%s", num);


    You need "%ld" instead of "%s" here.

    > printf("%s\n", buf);


    > return (0);
    > }


    I guess you're on the safe side if you use

    SIZE = ( sizeof( long ) * CHARBITS ) / 3 + 2

    (at least it seems to fit for 16, 32, 64, 128 and 256 bit longs as well
    as some intermediate values I tried).
    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| -berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oerring
    -berlin.de, Aug 14, 2003
    #3
  4. On Thu, 14 Aug 2003, David Hill wrote:

    > Hello -
    > What value should SIZE have?
    >
    > int
    > main(void)
    > {
    > long num = 1343331L;
    > char buf[SIZE];
    >
    > snprintf(buf, sizeof(buf), "%s", num);


    The format string should be "%ld" since num is a long.

    > printf("%s\n", buf);
    >
    > return (0);
    > }


    You are basically asking what is the maximum number of digits in a long.
    The answer is dependent on the compiler you are using.

    If you look in <limits.h> there is a macro CHAR_BIT that will give you the
    number of bits in a char. The sizeof operator can tell you the number of
    bytes in a long. Thus CHAR_BIT*sizeof(long) will give you the number of
    bits in a long.

    A number displayed in octal will always be equal to or longer than the
    same number displayed in decimal.

    There is no direct relationship between the number of bits and the number
    of decimal digits but there is a relationship between the number of bits
    and the number of octal digits. Three bits equals one octal digit. Thus
    CHAR_BIT*sizeof(long)/3 is the number of octal digits.

    Therefore a string of length CHAR_BIT*sizeof(long)/3+1 (plus one for the
    terminating null character) will be long enough.

    --
    main(){int j=1234;char t[]=":mad:abcdefghijklmnopqrstuvwxyz.\n",*i=
    "iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
    *i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}
    Darrell Grainger, Aug 14, 2003
    #4
  5. David Hill

    CBFalconer Guest

    -berlin.de wrote:
    > David Hill <> wrote:
    >
    > > What value should SIZE have?

    >
    > > int
    > > main(void)
    > > {
    > > long num = 1343331L;
    > > char buf[SIZE];

    >
    > > snprintf(buf, sizeof(buf), "%s", num);

    >
    > You need "%ld" instead of "%s" here.
    >
    > > printf("%s\n", buf);
    > > return (0);
    > > }

    >
    > I guess you're on the safe side if you use
    > SIZE = ( sizeof( long ) * CHARBITS ) / 3 + 2
    > (at least it seems to fit for 16, 32, 64, 128 and 256 bit longs
    > as well as some intermediate values I tried).


    In his example (corrected) he only needs 8. "1343331\0" will
    result. However it might be better to take advantage of the
    return value from snprintf and dynamically allocate the space. In
    addition, I believe snprintf is a C99 feature, not C90. From
    N869:

    7.19.6.5 The snprintf function

    Synopsis
    [#1]
    #include <stdio.h>
    int snprintf(char * restrict s, size_t n,
    const char * restrict format, ...);

    Description

    [#2] The snprintf function is equivalent to fprintf, except
    that the output is written into an array (specified by
    argument s) rather than to a stream. If n is zero, nothing
    is written, and s may be a null pointer. Otherwise, output
    characters beyond the n-1st are discarded rather than being
    written to the array, and a null character is written at the
    end of the characters actually written into the array. If
    copying takes place between objects that overlap, the
    behavior is undefined.

    Returns

    [#3] The snprintf function returns the number of characters
    that would have been written had n been sufficiently large,
    not counting the terminating null character, or a negative
    value if an encoding error occurred. Thus, the null-
    terminated output has been completely written if and only if
    the returned value is nonnegative and less than n.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Aug 14, 2003
    #5
  6. David Hill

    Kevin Easton Guest

    -berlin.de wrote:
    > David Hill <> wrote:
    >> What value should SIZE have?

    >
    >> int
    >> main(void)
    >> {
    >> long num = 1343331L;
    >> char buf[SIZE];

    >
    >> snprintf(buf, sizeof(buf), "%s", num);

    >
    > You need "%ld" instead of "%s" here.
    >
    >> printf("%s\n", buf);

    >
    >> return (0);
    >> }

    >
    > I guess you're on the safe side if you use
    >
    > SIZE = ( sizeof( long ) * CHARBITS ) / 3 + 2


    ITYM CHAR_BIT, which you'll have to #include <limits.h> to get.

    - Kevin.
    Kevin Easton, Aug 14, 2003
    #6
  7. David Hill

    Jeff Guest

    "David Hill" <> wrote in message
    news:...
    > Hello -
    > What value should SIZE have?
    >
    > int
    > main(void)
    > {
    > long num = 1343331L;
    > char buf[SIZE];
    >
    > snprintf(buf, sizeof(buf), "%s", num);


    If you mean:

    snprintf(buf, sizeof(buf), "%ld", num);

    Then you are printing the long integer in decimal form. Since the LONG_MAX
    is defined as 2147483647, you only need 10 digit to represent long integer.
    The SIZE should be 10+1 (1 for null character).


    > printf("%s\n", buf);
    >
    > return (0);
    > }


    --
    Jeff
    Jeff, Aug 14, 2003
    #7
  8. David Hill

    pete Guest

    Lew Pitcher wrote:

    > The standard doesnt say much about the /size/ of a long integer.
    > It mostly talks about the /range of values/
    > that a long int must be able to represent.


    Unfortunately, the part of the standard
    which discusses the ranges of integer types,
    is labeled as: "Sizes of integer types"
    pete, Aug 14, 2003
    #8
  9. David Hill

    Dan Pop Guest

    In <> (Darrell Grainger) writes:

    >If you look in <limits.h> there is a macro CHAR_BIT that will give you the
    >number of bits in a char. The sizeof operator can tell you the number of
    >bytes in a long. Thus CHAR_BIT*sizeof(long) will give you the number of
    >bits in a long.
    >
    >A number displayed in octal will always be equal to or longer than the
    >same number displayed in decimal.
    >
    >There is no direct relationship between the number of bits and the number
    >of decimal digits but there is a relationship between the number of bits
    >and the number of octal digits. Three bits equals one octal digit. Thus
    >CHAR_BIT*sizeof(long)/3 is the number of octal digits.
    >
    >Therefore a string of length CHAR_BIT*sizeof(long)/3+1 (plus one for the
    >terminating null character) will be long enough.


    You may also want to reserve place for a minus sign, in the general case.
    For the typical 32-bit long, your formula gives 11, which is not enough
    for LONG_MIN (sign + 10 digits + null character).

    To also cover the worst case (an 8 or 11-bit signed char), you have
    to also add 1 to the number of bits, before division:

    (CHAR_BIT * sizeof(type) + 1) / 3 + 2

    This is a constant expression, so it can be used to declare
    statically/automatically allocated buffers.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Aug 14, 2003
    #9
  10. "Jeff" <> writes:
    > "David Hill" <> wrote in message
    > news:...
    > > Hello -
    > > What value should SIZE have?
    > >
    > > int
    > > main(void)
    > > {
    > > long num = 1343331L;
    > > char buf[SIZE];
    > >
    > > snprintf(buf, sizeof(buf), "%s", num);

    >
    > If you mean:
    >
    > snprintf(buf, sizeof(buf), "%ld", num);
    >
    > Then you are printing the long integer in decimal form. Since the LONG_MAX
    > is defined as 2147483647, you only need 10 digit to represent long integer.
    > The SIZE should be 10+1 (1 for null character).


    LONG_MAX may be defined as 2147483647 in your implementation, but
    that's just a lower bound. I've used a number of implementations with
    64-bit longs, making LONG_MAX 9223372036854775807L (2**63-1). Other
    values are possible. Write code assuming that longs are at most 10
    digits, and it will break when you try to port it -- probably silently
    and at the worst possible moment.

    And you forgot to allow room for the sign.

    (If I were to take the question literally (allowing for the error in
    the format and the missing "#include <stdio.h>"), I'd say that SIZE
    can be as small as 8, since the program only writes the value 1343331L
    into the buffer. Presumably the OP wanted to allow for any possible
    long value, though.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    Keith Thompson, Aug 14, 2003
    #10
  11. David Hill <> wrote in message news:<>...
    > Hello -
    > What value should SIZE have?


    #include <limits.h>
    #include <stdio.h>

    #define SIZE(stype) (sizeof(stype) * CHAR_BIT * 12655ul / 42039 + 1)

    > int
    > main(void)
    > {
    > long num = 1343331L;
    > char buf[SIZE];


    char buf[SIZE(long) + 1]; /* +1 for '\0' */

    >
    > snprintf(buf, sizeof(buf), "%s", num);


    sprintf(buf, "%ld", num);

    > printf("%s\n", buf);
    >
    > return (0);
    > }


    --
    Peter
    Peter Nilsson, Aug 15, 2003
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    657
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,174
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    458
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    778
    Peter Nilsson
    Jan 15, 2008
  5. veryhotsausage
    Replies:
    1
    Views:
    1,773
    veryhotsausage
    Jul 4, 2008
Loading...

Share This Page