When I write a string such as the following:
sprintf(java_script,"Step: <INPUT NAME=\"angle\" TYPE=\"text\" value=\"%f\" SIZE="20">", floatVal);
It puts an "f" in the input field.
However, if I create another string and put it in that string, it will print the desired result.
For example:
char fnum[20];
sprintf(fnum,"%f",floatVal);
sprintf(java_script,"Step: <INPUT NAME=\"angle\" TYPE=\"text\" value=\"%s\" SIZE="20">", fnum);
This works.
Any ideas?
sprintf formatting question
- Chris Ruff
- Posts: 222
- Joined: Thu Apr 24, 2008 4:09 pm
- Location: topsail island, nc
- Contact:
Re: sprintf formatting question
It's good that it works.
You have found a compiler limitation (bug?), I run into that every now and then, usually when doing something all-in-one-line.
If you have a line like:
sprintf(filbert,"%s%s%s",getfilb1(),getfilb2(),getfilb3());
there is no guarantee that the compiled code will execute getfilb1 then 2 then 3 in that order. It may execute getfilb3() first.
That one was a surprise when I wrote code that required them to be in order!
Chris
You have found a compiler limitation (bug?), I run into that every now and then, usually when doing something all-in-one-line.
If you have a line like:
sprintf(filbert,"%s%s%s",getfilb1(),getfilb2(),getfilb3());
there is no guarantee that the compiled code will execute getfilb1 then 2 then 3 in that order. It may execute getfilb3() first.
That one was a surprise when I wrote code that required them to be in order!
Chris
Real Programmers don't comment their code. If it was hard to write, it should be hard to understand
Re: sprintf formatting question
try using the %f with formatting. such as %4.3f i have found that not using the formatting can also cause problems at times.
Re: sprintf formatting question
Are you sure its sprintf not siprintf?
siprintf does not include floating point stuff.
The normal NNDK (anything other than SBL2E and MOD5213) uses standard newlib code.
The SBL2e and MOD5213 uses a much smaller more efficent printf core written by netburner.
They should be 100% identical, but its possible we have a weird bug.
So what platform and NNDK version do you have the error on?
siprintf does not include floating point stuff.
The normal NNDK (anything other than SBL2E and MOD5213) uses standard newlib code.
The SBL2e and MOD5213 uses a much smaller more efficent printf core written by netburner.
They should be 100% identical, but its possible we have a weird bug.
So what platform and NNDK version do you have the error on?
Re: sprintf formatting question
re:
Also, doesn't NetBurner support the safe version snprintf? Shouldn't everyone that doesn't use the C++ iostream library being using the safe versions instead?
And maybe finally something useful: what you posted shouldn't even compile. Did you cut and paste or do it from memory? You're missing escapes on the final size = 20. When I correct those your code compiles and works for me on a MOD5272. Here's the code
Here's an image of the output:
Sure, use the C++ iostream library instead. (sorry, I have a reputation to uphold).Any ideas?
Also, doesn't NetBurner support the safe version snprintf? Shouldn't everyone that doesn't use the C++ iostream library being using the safe versions instead?
And maybe finally something useful: what you posted shouldn't even compile. Did you cut and paste or do it from memory? You're missing escapes on the final size = 20. When I correct those your code compiles and works for me on a MOD5272. Here's the code
Code: Select all
const int MAX_MSG_SIZE=100;
char msg[MAX_MSG_SIZE];
float float_val = 45.6;
snprintf(msg, MAX_MSG_SIZE,"Step: <INPUT NAME=\"angle\" TYPE=\"text\" value=\"%f\" SIZE=\"20\">", float_val);
printf ("%s", msg);
- Attachments
-
- snprintfResult.png (7.09 KiB) Viewed 4275 times