size of long

D

David Hill

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);
}
 
L

Lew Pitcher

David said:
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)
 
J

Jens.Toerring

David Hill said:
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
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
D

Darrell Grainger

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.
 
C

CBFalconer

David Hill said:
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.
 
K

Kevin Easton

J

Jeff

David Hill said:
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).
 
P

pete

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"
 
D

Dan Pop

In said:
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
 
K

Keith Thompson

Jeff said:
David Hill said:
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.)
 
P

Peter Nilsson

David Hill said:
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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top