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

putc and printf

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



Joined: 05 Jun 2013
Posts: 14

View user's profile Send private message

putc and printf
PostPosted: Fri Oct 09, 2015 12:20 am     Reply with quote

Hi,
I have a very simple query. I want to send a number using serial port.
e.g
Quote:

int a=1;

i want to receive 01 instead of 1 at the receiver side.for this purpose i use putc command like that
Code:

putc(a);

but nothing displayed.
then i tried printf
Code:

printf("%u",a);

but only 1 is printed out. then i tried
Code:

printf("%x",a);

Then i get 01

I want to know is it the right way to send this?
Also what is the purpose of putc command. Is it only print the characters but not interger?
Ttelmah



Joined: 11 Mar 2010
Posts: 19485

View user's profile Send private message

PostPosted: Fri Oct 09, 2015 12:46 am     Reply with quote

Probably not.

Your %x, prints the number in hexadecimal. If you number gets to 10 (decimal), it'll output as 0a. Is this what you want?....

Start at the beginning '1' (number), sent using putc, is the ASCII character 'SOH' which means 'start of heading'. Not the number '0' (this is ASCII 0x31).
You don't want to use putc to send numbers unless you have already turned them into ASCII.

Printf will turn number into ASCII for you. %d does this exactly as you have found, but '1', is the digit "1", not the two digits "01". The answer to this is in the manual for printf (read a C textbook which will explain much more). You can use: %02d which says 'print this result in a field 2 digits wide, with leading zero(s)'. So:

printf("%02d",a);

will output "01", and when 'a' gets to 10, will output "10". When a is '0' this will output "00".

Probably what you want.
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