Reynard 0 Posted February 10, 2018 Report Share Posted February 10, 2018 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 Quote Link to post Share on other sites
JorgeF 0 Posted February 13, 2018 Report Share Posted February 13, 2018 (edited) 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 February 16, 2018 by JorgeF Quote Link to post Share on other sites
Reynard 0 Posted February 13, 2018 Author Report Share Posted February 13, 2018 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 Quote Link to post Share on other sites
JorgeF 0 Posted February 16, 2018 Report Share Posted February 16, 2018 (edited) 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 February 16, 2018 by JorgeF Quote Link to post Share on other sites
Reynard 0 Posted February 16, 2018 Author Report Share Posted February 16, 2018 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 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.