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

Problems with printf or INT/INT8

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







Problems with printf or INT/INT8
PostPosted: Mon Apr 21, 2003 8:34 am     Reply with quote

I have had some difficulty lately with either int or int8 acting like a 7 bit or a signed 8 bit when using printf with an LCD.

I will get the correct values up to 127, after that the values switch to -128 and then work back to 127 for the full count of 255. I have tried similar experiments with just lcd_putc without the same problem yet I have not found any similar problems posted on this forum with this specificity.

Why is this happening? The code is *very* simple ...

Disclaimer:
I am citing this code from memory and not pasteing the *actual* code so any typos should not necessarily be construed as the *real* problem.

void main () {

int i=0

while (TRUE) {
for (i=32;i<256;i++) {
printf (lcd_putc,"Char Dec Hex\n");
printf (lcd_putc,"\%c \%d \%X",i,i,i);
delay_ms (1000);
lcd_putc("\f");
} // End For
} // End While

} // End Main
___________________________
This message was ported from CCS's old forum
Original Post ID: 13865
R.J.Hamlett
Guest







Re: Problems with printf or INT/INT8
PostPosted: Mon Apr 21, 2003 8:50 am     Reply with quote

:=I have had some difficulty lately with either int or int8 acting like a 7 bit or a signed 8 bit when using printf with an LCD.
:=
:=I will get the correct values up to 127, after that the values switch to -128 and then work back to 127 for the full count of 255. I have tried similar experiments with just lcd_putc without the same problem yet I have not found any similar problems posted on this forum with this specificity.
:=
:=Why is this happening? The code is *very* simple ...
:=
:=Disclaimer:
:=I am citing this code from memory and not pasteing the *actual* code so any typos should not necessarily be construed as the *real* problem.
:=
:=void main () {
:=
:= int i=0
:=
:= while (TRUE) {
:= for (i=32;i<256;i++) {
:= printf (lcd_putc,"Char Dec Hex\n");
:= printf (lcd_putc,"\%c \%d \%X",i,i,i);
:= delay_ms (1000);
:= lcd_putc("\f");
:= } // End For
:= } // End While
:=
:=} // End Main
This is happening, because you are using the wrong qualifier in the printf statement. \%d, tells the function to expect a _signed_ integer. Hence the eighth bit will be treated as a sign bit. The qualifier to print an unsigned integer, is \%u...

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 13868
KeithWard
Guest







Re: Problems with printf or INT/INT8
PostPosted: Mon Apr 21, 2003 11:38 am     Reply with quote

Nice catch! So much for my memory of C and RTFM!

Thanks!

:=:=I have had some difficulty lately with either int or int8 acting like a 7 bit or a signed 8 bit when using printf with an LCD.
:=:=
:=:=I will get the correct values up to 127, after that the values switch to -128 and then work back to 127 for the full count of 255. I have tried similar experiments with just lcd_putc without the same problem yet I have not found any similar problems posted on this forum with this specificity.
:=:=
:=:=Why is this happening? The code is *very* simple ...
:=:=
:=:=Disclaimer:
:=:=I am citing this code from memory and not pasteing the *actual* code so any typos should not necessarily be construed as the *real* problem.
:=:=
:=:=void main () {
:=:=
:=:= int i=0
:=:=
:=:= while (TRUE) {
:=:= for (i=32;i<256;i++) {
:=:= printf (lcd_putc,"Char Dec Hex\n");
:=:= printf (lcd_putc,"\%c \%d \%X",i,i,i);
:=:= delay_ms (1000);
:=:= lcd_putc("\f");
:=:= } // End For
:=:= } // End While
:=:=
:=:=} // End Main
:=This is happening, because you are using the wrong qualifier in the printf statement. \%d, tells the function to expect a _signed_ integer. Hence the eighth bit will be treated as a sign bit. The qualifier to print an unsigned integer, is \%u...
:=
:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 13875
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