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

Formatting in printf

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



Joined: 18 Nov 2013
Posts: 160

View user's profile Send private message

Formatting in printf
PostPosted: Mon Mar 02, 2015 1:32 pm     Reply with quote

I'm using printf() to output some debug statements. But the float format does not seem to work correctly.

Code:

float val = 100.974;
printf("%.2f\r\n", val);

Printout = "100.00"

I even tried the 'g' format, but no change.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 2:05 pm     Reply with quote

What PIC and what compiler version ? Always post those.
SeeCwriter



Joined: 18 Nov 2013
Posts: 160

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 2:30 pm     Reply with quote

Sorry. An 18F8722 with V5.042 PCWHD.
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 2:39 pm     Reply with quote

Try it with a minimum width. A lot of CCS versions do not like undefined widths.
So %3.2f which is the minimum that could hold the .xx part of the number. It'll expand as needed.
SeeCwriter



Joined: 18 Nov 2013
Posts: 160

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 2:43 pm     Reply with quote

Didn't help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 2:44 pm     Reply with quote

Quote:
18F8722 with V5.042 PCWHD.

If I run a test program with that version in MPLAB simulator, I get
this output:
Quote:
100.97

Test program:
Code:

#include <18F8722.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

//==============================
void main()
{
float val = 100.974;

printf("%.2f\r\n", val);

while(TRUE);
}
SeeCwriter



Joined: 18 Nov 2013
Posts: 160

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 2:53 pm     Reply with quote

That worked.
SeeCwriter



Joined: 18 Nov 2013
Posts: 160

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 3:11 pm     Reply with quote

But back to my program, even doing the following doesn't change the output.
Code:

char str[8];
sprint( str, "%3.2f", val );
printf("My val: %s\r\n", str );

Output = 100.00

I can break just before printf() and mouse over 'val' and it shows the correct value of 100.974.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 4:10 pm     Reply with quote

I don't know anything about mousing over. I never do that.

If I run the test program below in MPLAB Simulator (MPLAB vs. 8.92),
I get the following output displayed in the Sim UART1 window:
Quote:
My val: 100.97

Test program:
Code:

#include <18F8722.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

//==============================
void main()
{
float val = 100.974;

char str[8];
sprintf( str, "%3.2f", val );
printf("My val: %s\r\n", str );

while(TRUE);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Tue Mar 03, 2015 8:31 am     Reply with quote

I'd be suspicious of a simulator problem. Except for some very old V4 compilers which give problems without a field width, everything I've tried works OK....
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