|
|
View previous topic :: View next topic |
Author |
Message |
Sherpa Doug Guest
|
printf question |
Posted: Wed Nov 27, 2002 3:34 pm |
|
|
What is the difference between:
fprintf(fish,"$04lu\n\r",Temp);
and
fprintf(fish,"$4lu\n\r",Temp);
The manual says the 4 specifies how many characters are to be outputted, or 04 to indicate leading zeros.... So if I use just 4 do I get leading spaces? how does it generate 4 characters?
___________________________
This message was ported from CCS's old forum
Original Post ID: 9527 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: printf question |
Posted: Wed Nov 27, 2002 3:54 pm |
|
|
:=What is the difference between:
:=
:=fprintf(fish,"$04lu\n\r",Temp);
:=and
:=fprintf(fish,"$4lu\n\r",Temp);
:=
:=The manual says the 4 specifies how many characters are to be outputted, or 04 to indicate leading zeros.... So if I use just 4 do I get leading spaces? how does it generate 4 characters?
----------------------------------------------
The program given below, generated this output:
<PRE>
0123
123
</PRE>
The first line has a leading 0, and the 2nd line has a leading
space.
#include "c:\program files\picc\devices\16F877.h"
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT, NOLVP
#use Delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, STREAM=Fish)
//======================================
main()
{
long Temp;
Temp = 123;
fprintf(fish,"\%04lu\n\r",Temp);
fprintf(fish,"\%4lu\n\r",Temp);
while(1);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 9529 |
|
|
Ed Arnold Guest
|
Re: printf question |
Posted: Wed Nov 27, 2002 3:56 pm |
|
|
:=What is the difference between:
:=
:=fprintf(fish,"$04lu\n\r",Temp);
:=and
:=fprintf(fish,"$4lu\n\r",Temp);
:=
:=The manual says the 4 specifies how many characters are to be outputted, or 04 to indicate leading zeros.... So if I use just 4 do I get leading spaces? how does it generate 4 characters?
:=
I have used this on many projects and you are correct "\%04lu" will give you leading zeros and "\%4lu" will in fact give leading spaces, at least on an LCD screen. Sort of like right justifying the output. I have not tried this for outputting on a serial stream. By the way, I think you meant to use '\%' signs in your code instead of '$'. Hope this helps.
Ed Arnold
___________________________
This message was ported from CCS's old forum
Original Post ID: 9530 |
|
|
david smith Guest
|
Re: printf formatting |
Posted: Wed Nov 27, 2002 5:38 pm |
|
|
:=What is the difference between:
Note also, at least with this compiler,
\%04Lu is interpreted the same, and the L is not misread as 1.
Also, before you try, the leading zero doesn't work for floats.
___________________________
This message was ported from CCS's old forum
Original Post ID: 9538 |
|
|
Phil G Guest
|
Re: printf question |
Posted: Wed Nov 27, 2002 8:22 pm |
|
|
:=:=What is the difference between:
:=:=
:=:=fprintf(fish,"$04lu\n\r",Temp);
:=:=and
:=:=fprintf(fish,"$4lu\n\r",Temp);
:=:=
:=:=The manual says the 4 specifies how many characters are to be outputted, or 04 to indicate leading zeros.... So if I use just 4 do I get leading spaces? how does it generate 4 characters?
:=----------------------------------------------
:=
:=The program given below, generated this output:
:=
:=<PRE>
:=0123
:= 123
:=</PRE>
:=
:=The first line has a leading 0, and the 2nd line has a leading
:=space.
:=
:=
:=#include "c:\program files\picc\devices\16F877.h"
:=#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT, NOLVP
:=#use Delay(clock=8000000)
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, STREAM=Fish)
:=//======================================
:=main()
:={
:=long Temp;
:=Temp = 123;
:=
:=fprintf(fish,"\%04lu\n\r",Temp);
:=fprintf(fish,"\%4lu\n\r",Temp);
:=
:=while(1);
:=}
What is the output if Temp = 12345 ?
___________________________
This message was ported from CCS's old forum
Original Post ID: 9545 |
|
|
Tomi Guest
|
Re: printf question |
Posted: Thu Nov 28, 2002 1:54 am |
|
|
Not sure about CCS C but the original C printf definition says that your format expresssion must long enough to fit the number. So in a legal C the output for 12345 is "12345" regardless of the "\%04lu" format (the printf could not truncate the number). And maybe this is the reason why we have problems with printing floats.
:=:=:=What is the difference between:
:=:=:=
:=:=:=fprintf(fish,"$04lu\n\r",Temp);
:=:=:=and
:=:=:=fprintf(fish,"$4lu\n\r",Temp);
:=:=:=
:=:=:=The manual says the 4 specifies how many characters are to be outputted, or 04 to indicate leading zeros.... So if I use just 4 do I get leading spaces? how does it generate 4 characters?
:=:=----------------------------------------------
:=:=
:=:=The program given below, generated this output:
:=:=
:=:=<PRE>
:=:=0123
:=:= 123
:=:=</PRE>
:=:=
:=:=The first line has a leading 0, and the 2nd line has a leading
:=:=space.
:=:=
:=:=
:=:=#include "c:\program files\picc\devices\16F877.h"
:=:=#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT, NOLVP
:=:=#use Delay(clock=8000000)
:=:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, STREAM=Fish)
:=:=//======================================
:=:=main()
:=:={
:=:=long Temp;
:=:=Temp = 123;
:=:=
:=:=fprintf(fish,"\%04lu\n\r",Temp);
:=:=fprintf(fish,"\%4lu\n\r",Temp);
:=:=
:=:=while(1);
:=:=}
:=
:=What is the output if Temp = 12345 ?
___________________________
This message was ported from CCS's old forum
Original Post ID: 9557 |
|
|
|
|
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
|