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() format question?

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



Joined: 25 Aug 2005
Posts: 65
Location: Huntington Beach, CA

View user's profile Send private message

printf() format question?
PostPosted: Wed Oct 12, 2005 12:10 am     Reply with quote

The following statement prints what follows to the monitor.

printf("Voltage=%f volts\n\r",volts);
Voltage=1.344628 volts

When I add the format %1.3f........as follows, I get the next output line.

printf("Voltage=%1.3f volts\n\r",volts);
Voltage=



0000001.344 volts

Thats 247 spaces and 5 leading zeros. I have tried numerous variations but cannot get what one would expect.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 12, 2005 1:00 am     Reply with quote

It's been buggy for a while. I did a few experiments just now,
and it looks like the "width" value must be greater than the
"precision" value to avoid the bug.
i.e., The value on the left side of the decimal point must be greater
than the value on the right side.

Example:
The following code displays: Voltage=1.344 volts
Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//================================
void main()
{
float Voltage;

Voltage=1.344628;

printf("Voltage=%4.3f volts\n\r", Voltage);


while(1);
}
Ttelmah
Guest







PostPosted: Wed Oct 12, 2005 2:43 am     Reply with quote

Though printf is buggy, what is being tried in the original post, is 'asking for trouble'. The number in front of the DP, is the 'total field width', while the number after the DP is the number of digits after the decimal point. Most compilers are smart enugh, that if you ask for a 'field width' of one digit, and three digits after the DP, they increase the first number for you, when decimals are being printed. What happens with CCS at the moment is that the arithmetic 'wraps', giving 256-4 (three decimals, plus the DP itself) digits in front of the DP...
So though it is a fault, it is using the field definitions in a 'abnormal' way, that causes it. So long as you ensure that the total field width exceeds the digits required after the DP (and include space for the DP itself), the problem does not appear.

Best Wishes
Guest








PostPosted: Wed Oct 12, 2005 1:57 pm     Reply with quote

Ahhh yes. I referred to Kernigan and Ritchie and found the description. The CCS manual only has one line devoted to this statement and doesn't fully explain it. I guess the period doesn't count in the number of digits.

Its been about 15 years since my one and only C class.

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