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

(f)printf format %u %d broken ?

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



Joined: 09 Feb 2008
Posts: 7

View user's profile Send private message

(f)printf format %u %d broken ?
PostPosted: Sat Feb 09, 2008 10:01 am     Reply with quote

Compiler PCD 4.068

#include <...30F6014.h>
#fuses XT_PLL16
#use delay(clock=120000000,restart_wdt)
#use rs232(baud=38400,UART2,ERRORS,STREAM=seriale)

void main()
{
unsigned int16 x;
signed int16 y;

x=60000;
fprintf(seriale,"%u ",x);
y=-1000;
fprintf(seriale,"%d ",y);

while(1);
}


the result on RS232 is
60000 64536

but...

It would be
60000 -1000

or not?

Of course I tried a lot of %u %d &ld %lu signed unsigned casting....before write...

Regards
Daniele
Guest








PostPosted: Sat Feb 09, 2008 10:41 am     Reply with quote

My manual says -

Unsigned int (8 bits) = %u
Signed int (8 bits) = %d

Long Unsigned int (16 or 32 bits) = %Lu
Long Signed int (16 or 32 bits) = %Ld

(Thanks to PCM programmer for clarifying that a long in CCS printf terms is a 16 or 32 bit value).

HTH - Steve H.
JoaoSantos



Joined: 19 Jun 2007
Posts: 20
Location: Castelo Branco, Portugal

View user's profile Send private message Visit poster's website

PostPosted: Sat Feb 09, 2008 5:14 pm     Reply with quote

Hello,
I hit a similar question here in this topic
http://www.ccsinfo.com/forum/viewtopic.php?t=33599

In my case seems it is a bug from PCWHD.
The only way that I can work it out is printing to a string first using printf, and that making a fprint %s, of that string.

Hope it helps
regards,
Daniele



Joined: 09 Feb 2008
Posts: 7

View user's profile Send private message

PostPosted: Sun Feb 10, 2008 4:21 am     Reply with quote

Hi,

how I can use printf to write in a string?

if the bug is in printf how using this function in other way skip it?

regards.
Daniele



Joined: 09 Feb 2008
Posts: 7

View user's profile Send private message

PostPosted: Sun Feb 10, 2008 4:28 am     Reply with quote

look this

...
x=(unsigned int16)60000;
printf("%Lu ",x);
y=(signed int16)-1000;
printf("%Ld ",y);
... this print "60000 64536 "

...
y=(signed int16)-1000;
printf("%Ld ",y);
x=(unsigned int16)60000;
printf("%Lu ",x);
... this print "-1000 -5536 "

seems that the first formatting will be forced to the second...

Daniele
esa



Joined: 31 Jan 2008
Posts: 19

View user's profile Send private message

PostPosted: Sun Feb 10, 2008 5:47 am     Reply with quote

I try your code with PCH V4.055
And good working

You can see here, PCD is in develpoment and some error about printf :
http://www.ccsinfo.com/content.php?page=pcdstatus

Maybee, you can send a email directly to the CCS official support.

Regards
Eric
Daniele



Joined: 09 Feb 2008
Posts: 7

View user's profile Send private message

PostPosted: Sun Feb 10, 2008 7:28 am     Reply with quote

unfortunally my PCD account start with 4.066 version (and it doesn't work)
and it's not possible download old version....

regards
Daniele
JoaoSantos



Joined: 19 Jun 2007
Posts: 20
Location: Castelo Branco, Portugal

View user's profile Send private message Visit poster's website

PostPosted: Sun Feb 10, 2008 8:28 am     Reply with quote

try something like this:
Code:

#use rs232(UART1,baud=9600, parity=N,bits=8,STREAM = Debug_Decoder)
....

char texto[30];

...

fprintf(texto,"%u %ld", ..);
fprintf(Debug_Decoder, texto);

...


It worked for me...
Daniele



Joined: 09 Feb 2008
Posts: 7

View user's profile Send private message

PostPosted: Sun Feb 10, 2008 5:56 pm     Reply with quote

no It doesn't work fot me...

#use rs232(baud=38400,UART2,ERRORS,STREAM=seriale)
char stringa[30];

x=(unsigned int16)60000;
y=(signed int16)-1000;
fprintf(stringa,"%u %d",x,y); //*****
fprintf(seriale,stringa);

the line //****** give...

*** Error 130 "prova.c" Line 20(31,32): Stream must be a constant in the valid range ::


now I'll write my function to convert signed and unsigned to string ... too many time lost for this...
Daniele



Joined: 09 Feb 2008
Posts: 7

View user's profile Send private message

PostPosted: Sun Feb 10, 2008 5:58 pm     Reply with quote

I forgot...

thanks and regards for now

I'll write you if can found a solution...
Daniele



Joined: 09 Feb 2008
Posts: 7

View user's profile Send private message

PostPosted: Sun Feb 10, 2008 6:31 pm     Reply with quote

my solution ...
not so beautifull but 10 minutes long...

void printSigned(signed int d)
{
if (d<0) { putc('-'); d=d*(-1);}
else { putc(' '); }
putc(d/10000+'0');
d=d%10000;
putc(d/1000+'0');
d=d%1000;
putc(d/100+'0');
d=d%100;
putc(d/10+'0');
d=d%10;
putc(d+'0');
}

void printUnsigned(unsigned int d)
{
putc(' ');
putc(d/10000+'0');
d=d%10000;
putc(d/1000+'0');
d=d%1000;
putc(d/100+'0');
d=d%100;
putc(d/10+'0');
d=d%10;
putc(d+'0');
}
JoaoSantos



Joined: 19 Jun 2007
Posts: 20
Location: Castelo Branco, Portugal

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 11, 2008 3:17 am     Reply with quote

ok if it works, its ok.
I don't know i does it gives you an error. Try and take out the casts and put the printf with the l versios (lu and ld).
regards,
Ttelmah
Guest







PostPosted: Mon Feb 11, 2008 4:07 am     Reply with quote

Daniele wrote:
no It doesn't work fot me...

#use rs232(baud=38400,UART2,ERRORS,STREAM=seriale)
char stringa[30];

x=(unsigned int16)60000;
y=(signed int16)-1000;
fprintf(stringa,"%u %d",x,y); //*****
fprintf(seriale,stringa);

the line //****** give...

*** Error 130 "prova.c" Line 20(31,32): Stream must be a constant in the valid range ::


now I'll write my function to convert signed and unsigned to string ... too many time lost for this...


This is failing, because 'fprintf', requires a stream name. You are giving it a string storage area... Needs to be sprintf.

Best Wishes
JoaoSantos



Joined: 19 Jun 2007
Posts: 20
Location: Castelo Branco, Portugal

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 11, 2008 6:20 am     Reply with quote

Ttelmah wrote:
Daniele wrote:
no It doesn't work fot me...

#use rs232(baud=38400,UART2,ERRORS,STREAM=seriale)
char stringa[30];

x=(unsigned int16)60000;
y=(signed int16)-1000;
fprintf(stringa,"%u %d",x,y); //*****
fprintf(seriale,stringa);

the line //****** give...

*** Error 130 "prova.c" Line 20(31,32): Stream must be a constant in the valid range ::


now I'll write my function to convert signed and unsigned to string ... too many time lost for this...


This is failing, because 'fprintf', requires a stream name. You are giving it a string storage area... Needs to be sprintf.

Best Wishes


Yes that's right. Sorry for the mislead.
Embarassed
In my code I have sprintf...
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