View previous topic :: View next topic |
Author |
Message |
dynamitron
Joined: 18 Mar 2009 Posts: 38
|
printf question |
Posted: Sat Sep 22, 2012 11:36 am |
|
|
I have a float variable which can vary from -20.0 to 20.0
If I do a printf ("%5.1f",variable) the result is shifted left by one character when the value is negative.
Of course I could use a %05.1f but I do not like it because is less beautiful on a LCD display.
Any suggestion to avoid the shift and not 0 on the left ?
tanks !! |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Sep 22, 2012 3:36 pm |
|
|
Use an if() statement to test if the floating point value is positive.
If so, then display a space with an lcd_putc(' '); statement.
If it's negative, then don't display the space.
Then display the number using printf. Now it will not shift back and forth
on the LCD because of the minus sign. |
|
![](templates/subSilver/images/spacer.gif) |
dynamitron
Joined: 18 Mar 2009 Posts: 38
|
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9170 Location: Greensville,Ontario
|
|
Posted: Sun Sep 23, 2012 7:43 am |
|
|
When you have your project open, pressing F11, opens up the CCS C HELP screens..making for an instant reference to almost everything applicable to using CCS C.
This compiler is NOT C++, or VC or MS C so while some commands and functions are similar you can only use what is CCS C has.
Also if you open up any of the examples CCS supplies in the 'examples' folder, you'll see how to use most of the available functions to interface to most of the peripherals as well as external devices.
hth
jay |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun Sep 23, 2012 9:20 am |
|
|
Quote: | This compiler is NOT C++, or VC or MS C so while some commands and functions are similar you can only use what is CCS C has. |
CCS C is claiming ANSI/ISO C compatibility. The printf() format specifiers are part of the C standard, including the +/- "flags" and right/left justify rules.
Support for format specifiers is however not mentioned in the compliance matrix document: http://www.ccsinfo.com/downloads/ansi_compliance.pdf |
|
![](templates/subSilver/images/spacer.gif) |
dynamitron
Joined: 18 Mar 2009 Posts: 38
|
|
Posted: Sun Sep 23, 2012 9:31 am |
|
|
temtronic wrote: | When you have your project open, pressing F11, opens up the CCS C HELP screens..making for an instant reference to almost everything applicable to using CCS C.
This compiler is NOT C++, or VC or MS C so while some commands and functions are similar you can only use what is CCS C has.
Also if you open up any of the examples CCS supplies in the 'examples' folder, you'll see how to use most of the available functions to interface to most of the peripherals as well as external devices.
hth
jay |
Jay,
Of course I know all that but I would have expect that there was some sort of compatibility between the different "C" and as I was frustrated by my shifting number I googled on the c++ web page and I gave it as example... I could have also come across a ANSI C page. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Mon Sep 24, 2012 3:21 am |
|
|
Yes, this is one of the few bits of 'standard C', where CCS misses the mark.
The + modifier, is later (in ANSI, but not in the original K&R), but the - modifier for left justify, is in the very original K&R book, so they should at least do this, and this would give the required output.
The other one that is missing here, is ' '. This is meant to simply give a leading space, if the number is not negative.
Adding these would be pretty simple, so perhaps several of us should point this lack out to CCS.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Sep 24, 2012 5:13 am |
|
|
Quote: | If I do a printf ("%5.1f",variable) the result is shifted left by one character when the value is negative. |
I've been reviewing the original post and must confess, I'm not sure now what it's complaining about.
I don't see the result left shifted. It's right justified according to the C-specification. If the output string has less characters than the length specification, it's filled with spaces at the left. The only point is that the left justify option (flag "-") is missing, also flag "+".
Either there's a misunderstanding of the C-standard involved, or different compiler versions are behaving different.
Adding "-" flag support won't be bad, however. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Mon Sep 24, 2012 6:48 am |
|
|
All the versions I've tried generate _one_ leading space, and retain this whatever the number does. They do no 'justification' at all. So if you print 20, they display " 20.0", while if you send -20, they give " -20.0", moving the '20' right one digit.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Sep 25, 2012 12:14 am |
|
|
I see correct output format of %5.1f in all tested PCM and PCH versions:
" 20.0" one leading space
"-20.0" no leading space
" 2.0" two leading spaces
" -2.0" one leading space |
|
![](templates/subSilver/images/spacer.gif) |
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Tue Sep 25, 2012 1:10 am |
|
|
I've often wondered why the '-' isn't in printf for CCS as well.
It would be nice to see it in there.
I'd vote for support for this. -- Does CCS watch the forum enough that they'd see this?
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Tue Sep 25, 2012 1:35 am |
|
|
Don't think so.
As I say, this is in the original K&R book, so really 'ought' to be there.
I'm interested in Fvm's comment, and have been testing further. Appears there may be a bug in _sprintf_, rather than printf. I was using this, and viewing the result in a debugger.
Have just tried with printf, and agree with him, that the code _does_ correctly right justify, giving nicely aligned outputs as required.
Code: |
-20.0
20.0
-2.0
2.0
|
Output directly dumped from MPLAB.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Sep 25, 2012 9:54 am |
|
|
I had tested both printf() and sprintf() in PCH (V4.099 - 4.135) and PCM (my working version 4.127 only) and got coinciding results.
Code: | f=20.0;
printf("%5.1f\r\n",f);
sprintf(buf,"%5.1f\r\n",f);
f=-20.0;
printf("%5.1f\r\n",f);
sprintf(buf,"%5.1f\r\n",f);
f=2.0;
printf("%5.1f\r\n",f);
sprintf(buf,"%5.1f\r\n",f);
f=-2.0;
printf("%5.1f\r\n",f);
sprintf(buf,"%5.1f\r\n",f); |
I have no idea what causes different results. |
|
![](templates/subSilver/images/spacer.gif) |
|