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 support@ccsinfo.com

printf() formatting bug or feature?

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



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

printf() formatting bug or feature?
PostPosted: Wed Jul 08, 2009 9:15 pm     Reply with quote

Code:
int32 imW;
imW = 1234;
printf(lcd_putc, "\f\n%2.1wW", imW );    // 123.4W on the display.  Expected 1.2W
printf(lcd_putc, "\f\n%2.1wW", imW/100 );    // 1.2W on the display as expected

I wonder if this is a (minor) bug or a feature?

PCWH 4.081
PIC18F2620
_________________
Read the label, before opening a can of worms.


Last edited by kender on Wed Jul 08, 2009 10:11 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 08, 2009 9:57 pm     Reply with quote

The manual shows these two input values as examples:
Code:

%3.1w
Input:    18     254
Output:  1.8     25.4

This shows that the right-hand number in the format specifier controls the
number of digits to the right of the decimal point in the output.

For example, the code below has the following output:
Quote:
0.254

Code:
imW = 254;
printf("%2.3w\r", imW );
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Wed Jul 08, 2009 10:16 pm     Reply with quote

PCM programmer wrote:
This [the manual] shows that the right-hand number in the format specifier controls the number of digits to the right of the decimal point in the output.

I agree, right-hand number controls the number of digits to the right of the decimal point, as expected. But it looks like the left-hand number doesn't control the total field width (number of digits).
_________________
Read the label, before opening a can of worms.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 08, 2009 10:41 pm     Reply with quote

The printf specification says that the Width specifier is the minimum width.
If the number of digits is greater than the Width specifier, they will all be displayed, regardless of the Width value.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Wed Jul 08, 2009 10:56 pm     Reply with quote

PCM programmer wrote:
The printf specification says that the Width specifier is the minimum width.

I see - so, it's a feature.
Not to nitpick (the whole issue is minor and superficial), but that's not clear from compiler's online help. Minimum isn't mentioned there:
CCS compiler help wrote:
w
Unsigned int with decimal place inserted. Specify two numbers for n. The first is a total field width. The second is the desired number of decimal places.

_________________
Read the label, before opening a can of worms.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Jul 09, 2009 12:20 am     Reply with quote

As said, the optional width field in C format specifications is designating a minimum width. That's a common feature of the C language, not specific to CCS C. Otherwise, what do you expect in the said example, if it would mean a strict (maximum) width?

I addition, due to the fact, that "n" in n.m is specifying a total width (including the decimal point and following digits), there's no difference between "1.2" and "4.2", because 4 is the minimum width anyway.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Thu Jul 09, 2009 12:44 am     Reply with quote

FvM wrote:
Otherwise, what do you expect in the said example, if it would mean a strict (maximum) width?

From m.n (m > n) I was expecting to get a mantissa with n decimal places. Then %3.2w with 123, and %3.2w with 1230 would both produce 1.23. The calling code would keep track of the exponent separately (e.g. output the exponent separately).

Again, it works either way. In fact, now (few hours after the O.P.) I like the way it's currently done better.
_________________
Read the label, before opening a can of worms.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Jul 09, 2009 4:21 am     Reply with quote

Because the %w fixed point format is a CCS private extension of the C standard, it can involve arbitrary definitions. But the present way is plausible according to the minĂ­mum width meaning of the C standard format specifiers, I think.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Thu Jul 09, 2009 10:04 am     Reply with quote

FvM wrote:
Because the %w fixed point format is a CCS private extension of the C standard, it can involve arbitrary definitions.

*Smiling and firing the pilot light on the flamethrower.* Ah-ha. Somebody's got a flexible opinion. Look at this line:
FvM wrote:
That's a common feature of the C language, not specific to CCS C.

_________________
Read the label, before opening a can of worms.
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