Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > size of long

Reply
Thread Tools

size of long

 
 
David Hill
Guest
Posts: n/a
 
      08-14-2003
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);
}
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      08-14-2003
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)

 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      08-14-2003
David Hill <(E-Mail Removed)> 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
--
_ _____ _____
| ||_ _||_ _| http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
Reply With Quote
 
Darrell Grainger
Guest
Posts: n/a
 
      08-14-2003
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[]=":@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;}

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-14-2003
(E-Mail Removed)-berlin.de wrote:
> David Hill <(E-Mail Removed)> 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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Kevin Easton
Guest
Posts: n/a
 
      08-14-2003
(E-Mail Removed)-berlin.de wrote:
> David Hill <(E-Mail Removed)> 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.

 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      08-14-2003

"David Hill" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-14-2003
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"
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-14-2003
In <(E-Mail Removed)> http://www.velocityreviews.com/forums/(E-Mail Removed) (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: (E-Mail Removed)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-14-2003
"Jeff" <(E-Mail Removed)> writes:
> "David Hill" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
Use of Long and Long Long Bart C C Programming 27 01-15-2008 05:27 AM
long long and long Mathieu Dutour C Programming 4 07-24-2007 11:15 AM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments