What size are long and int?
If nID is a long being printed with an 'int' format and sizeof(int)
!= sizeof(long)
then you get misaligned data access (in the sense that the alignment is not correct) from there on.
Which compiler are you using? GCC should diagnose the problem without difficulty if that is the trouble. Similarly, if nID is a 'long long', you could have problems.
To answer your question - yes, there is an lower-bound on the upper-limit of the length of string that sprintf()
must be able to handle. The number is 509 for C89 systems, and 4095 for C99 systems.
To be precise, C99 says (fprintf()
, section §7.19.6.1, para 15):
The number of characters that can be produced by any single conversion shall be at least
4095.
There is no further qualification on sprintf()
.
With fixed version of amended example converted into compilable code:
#include <stdio.h>
int main(void)
{
char test[255] = {0};
int test1 = 2;
double test2 = 35.00;
int test3 = 0;
sprintf(test, "%d|%f|%d", test1, test2, test3);
puts(test);
return(0);
}
I get the output I would expect:
2|35.000000|0
To be getting the output you show, you have to be working with a very weird setup.
- What platform are you on?
The behaviour you are showing indicates that the sprintf()
function you are using is confused about the alignment or size of something. Do you have a prototype in scope - that is, have you #included <stdio.h>
? With GCC, I get all sorts of warnings when I omit the header:
x.c: In function ‘main’:
x.c:8: warning: implicit declaration of function ‘sprintf’
x.c:8: warning: incompatible implicit declaration of built-in function ‘sprintf’
x.c:9: warning: implicit declaration of function ‘puts’
But even with test2
redefined as a float
I get the correct result.
If I change test2
into a long double
, then I get a different set of warnings and a different result:
x.c: In function ‘main’:
x.c:8: warning: implicit declaration of function ‘sprintf’
x.c:8: warning: incompatible implicit declaration of built-in function ‘sprintf’
x.c:8: warning: format ‘%f’ expects type ‘double’, but argument 4 has type ‘long double’
x.c:9: warning: implicit declaration of function ‘puts’
2|0.000000|0
This is closer to what you are seeing, though far from identical.
Since we don't have the platform information, I'm suspicious that you are working with a truly ancient (or do I mean buggy?) version of C. But I'm still at something of a loss to see how you get what you show - unless you're on a big-endian machine (I'm testing on Intel Mac with MacOS X 10.6.2) ...pause... on a SPARC machine running Solaris 10, without #include <stdio.h>
and with long double test2 = 35.0;
, I get:
gcc -O x.c -o x && ./x
x.c: In function 'main':
x.c:8: warning: incompatible implicit declaration of built-in function 'sprintf'
2|-22446048024026502740613283801712842727785152907992454451420278635613183447049251888877319095301502091725503415850736723945766334416305449970423980980099172087880564051243997662107950451281495566975073444407658980167407319222477077473080454782593800009947058951029590025152409694784570786541673131117073399808.000000|0
That's different; it also generates 321 characters of output, so there's a buffer overflow in there - it is better to use 'snprintf()
' to prevent that from occurring. When things are declared properly, of course, I get the expected result.
So, can you post compilable code that shows your problem, instead of a snippet that does not? And can you identify your platform - machine type, operating system version, compiler version (and maybe C library version)?