|
|
View previous topic :: View next topic |
Author |
Message |
KeithWard Guest
|
Problems with printf or INT/INT8 |
Posted: Mon Apr 21, 2003 8:34 am |
|
|
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 |
Posted: Mon Apr 21, 2003 8:50 am |
|
|
:=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 |
Posted: Mon Apr 21, 2003 11:38 am |
|
|
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 |
|
|
|
|
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
|