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 question

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







printf question
PostPosted: Wed Nov 27, 2002 3:34 pm     Reply with quote

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

View user's profile Send private message

Re: printf question
PostPosted: Wed Nov 27, 2002 3:54 pm     Reply with quote

:=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
PostPosted: Wed Nov 27, 2002 3:56 pm     Reply with quote

:=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
PostPosted: Wed Nov 27, 2002 5:38 pm     Reply with quote

:=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
PostPosted: Wed Nov 27, 2002 8:22 pm     Reply with quote

:=:=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
PostPosted: Thu Nov 28, 2002 1:54 am     Reply with quote

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
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