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.