View previous topic :: View next topic |
Author |
Message |
Daniele
Joined: 09 Feb 2008 Posts: 7
|
(f)printf format %u %d broken ? |
Posted: Sat Feb 09, 2008 10:01 am |
|
|
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
|
|
Posted: Sat Feb 09, 2008 10:41 am |
|
|
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
|
|
Posted: Sat Feb 09, 2008 5:14 pm |
|
|
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
|
|
Posted: Sun Feb 10, 2008 4:21 am |
|
|
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
|
|
Posted: Sun Feb 10, 2008 4:28 am |
|
|
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
|
|
Posted: Sun Feb 10, 2008 5:47 am |
|
|
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
|
|
Posted: Sun Feb 10, 2008 7:28 am |
|
|
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
|
|
Posted: Sun Feb 10, 2008 8:28 am |
|
|
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
|
|
Posted: Sun Feb 10, 2008 5:56 pm |
|
|
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
|
|
Posted: Sun Feb 10, 2008 5:58 pm |
|
|
I forgot...
thanks and regards for now
I'll write you if can found a solution... |
|
|
Daniele
Joined: 09 Feb 2008 Posts: 7
|
|
Posted: Sun Feb 10, 2008 6:31 pm |
|
|
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
|
|
Posted: Mon Feb 11, 2008 3:17 am |
|
|
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
|
|
Posted: Mon Feb 11, 2008 4:07 am |
|
|
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
|
|
Posted: Mon Feb 11, 2008 6:20 am |
|
|
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.
In my code I have sprintf... |
|
|
|