A89: tigcc - some REAL routines this time
[Prev][Next][Index][Thread]
A89: tigcc - some REAL routines this time
These are much more suitable for use in real code (or should be - untested
again because I don;t have TI-GCC set up on my new PC yet), but still not
portable. If you want to use these routines on a platform that doesn't use
something close to standard ASCII characters where all the digits are in
order, then let me know so this can be modified.
Is my do-while loop syntax correct? I think I've condensed that a little
too much, but I do believe that it works AND this one will display a
one-digit zero if the number is zero. I'm proud of myself for optimizing C
code like this while learning all I know from reading 2 books and Niklas'
code, and it looks pretty too (does anyone have the URL of the Linux coding
format specifications handy? It just recommends K&R's format w/ position of
curly braces and 8-space tabs). Very clean code, if it works.
// -------------------------------------------------------------------------
// Int to String routines
// by Scott Noveck (scott@acz.org)
//
// These do NOT yet handle negative numbers and are NOT portable
// although that can be fixed easily if necessary.
//
// Two versions follow below:
// inttostring gives only used digits or a single zero if the input is zero.
// inttostring_fixed gives a fixed number of digits.
// -------------------------------------------------------------------------
#define STORAGE_SIZE 5 // largest int (65545) is five digits long
// function prototypes
*char inttostring(int num, char storage[]);
*char inttostring_fixed(int num, int digits, char storage[]);
char storage[STORAGE_SIZE+1] // Leaving this global since there's no main
// here, but you would pass it to the
// functions below
// inttostring
// Input: number to convert, char array to store string it
// Output: returns a pointer to the string _without_ leading zeros
// if num is zero will return string "0"
*char inttostring(int num, char storage[])
{
storage += STORAGE_SIZE+1;
do {
*(--storage) = num % 10 + '0';
} while (num/=10);
return(storage);
}
// inttostring_fixed
// Input: number to convert, number of digits, char array to store string
it
// Output: returns a pointer to the string with specified number of digits
*char inttostring_fixed(int num, int digits, char storage[])
{
char *storagetmp;
storagetmp = storage+STORAGE_SIZE+1;
do {
*(--storagetmp) = num % 10 + '0';
} while (num/=10);
return(storage+STORAGE_SIZE-digits);
}
// -------------------------------------------------------------------------
-Scott