Jump to content
Reynard

sprintf incorrect output

Recommended Posts

The sprint function is not outputting the expected formatted string.

	sprintf(strBuffer, "%+03d", 1);

This format is producing 0+1 and not +01 as expected.

Tested OK with gcc avr compiler.

SourceBoost C 7.42, Windows 10 64bit.

Interesting that this function takes an unsigned int value but will format a signed number.

Cheers

Reynard

 

Share this post


Link to post
Share on other sites

Hi

 

I'm not that sure that the constant value "1" is interpreted as unsigned int.

It will most probably be interpreted as (converted to) an "int" as "int" is the default format in "C".

Anyhow the "sprintf" function will take anything you throw as a parameter and will "interpret" each parameter acording to the format specifier on the formating string.

 

EDIT: Correction

The above aplies to the "printf/sprintf" familly of functions usually found on "C" compilers for desktop or high end microcontrollers, not to the limited implementation available in "BoostC".

Sorry for any incovenience.

 

 

Best regards

Jorge

 

 

Edited by JorgeF

Share this post


Link to post
Share on other sites

Hi Jorge,

It is confusing to the reader when the prototype says unsigned int but I suppose they had to say something as the function is non standard to normal C.

The output is still incorrect whether you specify +1 or -1 as the value.

Cheers

Reynard

 

Share this post


Link to post
Share on other sites

Hi

 

In my previous post I was refering to a generic "C" "sprintf" function.

Meanwhile I revised the "BoostC" manual and noticed that the "sprintf" implementation in "BoostC" is a very limited one.

 

I did some testing and it looks like the positive sign is beeing misplaced.

OTOH the function is only available for positive numbers (unsigned) so you may either drop the sign or just stuff it at the beginning of the generated string.

Something along this lines ....

buffer[0] = '+';
sprintf(buffer+1, "%02d", 1);

You may need to adjust field lenght or the order of statements for fine tunning your result.

 

HIH

 

 

EDIT ADD:

Besides the testing for this topic, I had never used "sprintf" in BoostC or XC8, simply because I consider it too heavy (code size and speed) for an 8 bit PIC.

I allways use the lightweight counterparts and add any needed handcrafted code for the specific formatting on a project by project basis.

 

Best regards

Jorge

 

Edited by JorgeF

Share this post


Link to post
Share on other sites

Hi Jorge,

I did my own implementation to get what I needed.

The sprint does do negative numbers as well but sign is still in the wrong place (0-1 for -1).

As you say it is a bit of a heavyweight.

I am running at 64MHz with 64k rom PIC18 so not much of a problem.

Cheers

Reynard

 

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×