CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

printf question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dynamitron



Joined: 18 Mar 2009
Posts: 38

View user's profile Send private message

printf question
PostPosted: Sat Sep 22, 2012 11:36 am     Reply with quote

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 !!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Sep 22, 2012 3:36 pm     Reply with quote

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.
dynamitron



Joined: 18 Mar 2009
Posts: 38

View user's profile Send private message

PostPosted: Sun Sep 23, 2012 5:41 am     Reply with quote

That what I was thinking to do !!

So, should I understand that the chapter "flag" of the following links does not apply in C CCS ? :
http://www.cplusplus.com/reference/clibrary/cstdio/printf/
If I use a flag + or - the compiler reports an error.
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Sep 23, 2012 7:43 am     Reply with quote

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
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Sep 23, 2012 9:20 am     Reply with quote

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
dynamitron



Joined: 18 Mar 2009
Posts: 38

View user's profile Send private message

PostPosted: Sun Sep 23, 2012 9:31 am     Reply with quote

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.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Mon Sep 24, 2012 3:21 am     Reply with quote

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
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Sep 24, 2012 5:13 am     Reply with quote

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.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Mon Sep 24, 2012 6:48 am     Reply with quote

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
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Sep 25, 2012 12:14 am     Reply with quote

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
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue Sep 25, 2012 1:10 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Tue Sep 25, 2012 1:35 am     Reply with quote

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
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Sep 25, 2012 9:54 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group