sprintf formatting question

Discussion to talk about software related topics only.
Post Reply
BMillikan
Posts: 14
Joined: Thu Dec 23, 2010 7:56 am

sprintf formatting question

Post by BMillikan »

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?
User avatar
Chris Ruff
Posts: 222
Joined: Thu Apr 24, 2008 4:09 pm
Location: topsail island, nc
Contact:

Re: sprintf formatting question

Post by Chris Ruff »

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
Real Programmers don't comment their code. If it was hard to write, it should be hard to understand
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: sprintf formatting question

Post by seulater »

try using the %f with formatting. such as %4.3f i have found that not using the formatting can also cause problems at times.
User avatar
pbreed
Posts: 1091
Joined: Thu Apr 24, 2008 3:58 pm

Re: sprintf formatting question

Post by pbreed »

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?
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: sprintf formatting question

Post by tod »

re:
Any ideas?
Sure, use the C++ iostream library instead. (sorry, I have a reputation to uphold).

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);
Here's an image of the output:
Attachments
snprintfResult.png
snprintfResult.png (7.09 KiB) Viewed 4284 times
Post Reply