??? 09/06/06 15:46 Read: times |
#123761 - A learning 'C' speed bump |
I've dug out my 15 year old Turbo C book and have re-learned quite alot, however I've run in to a problem I've been struggling with for a couple of days.
Here is my problem: In the below function if I declare the local variables as 'int' then the return value is only the last digit entered, for example this function is called, I enter '23' on the keypad, the return value is '3'. If I declare the local variables as 'char' then the return value is correct, but only up to 255. What am I missing for this to work for integers? Compliler is SDCC int getdec(unsigned char dpoint, unsigned char alwneg, unsigned char angent) // dpoint, alwneg, and angent are flags that are not implemented yet { // local variables int decimal; // return value int temp; // temporary value int key; ... ... ... key = 0; while (key == 0) { key = getkey(); // get key code from keypad } ... a switch/case statement assigns the correct numeric value to 'key' ... if ((key >= 0) & (key <= 9)) // if key is numeric { putchar(key + 48); // display ascii value temp = temp * 10 + key; // tally it } return(temp); } |
Topic | Author | Date |
A learning 'C' speed bump | 01/01/70 00:00 | |
Uninitialised? | 01/01/70 00:00 | |
Variables were initialized. | 01/01/70 00:00 | |
logical vs bitwise | 01/01/70 00:00 | |
this is when the C people try to be smart... | 01/01/70 00:00 | |
Ok, corrected the && ... | 01/01/70 00:00 | |
check the size of the variables | 01/01/70 00:00 | |
I Ceed it already | 01/01/70 00:00 | |
Style | 01/01/70 00:00 | |
style does not apply to C only | 01/01/70 00:00 | |
words | 01/01/70 00:00 | |
Does it?? | 01/01/70 00:00 | |
Verification | 01/01/70 00:00 | |
oh come on... | 01/01/70 00:00 | |
RESOLVED | 01/01/70 00:00 | |
variable scope | 01/01/70 00:00 |