On embedded systems, printf can sometimes drag and drop all floating point support for format strings such as %f .
More intelligent environments will make floating-point options for printf optional.
But even for integers in printf there is a lot of general purpose code, and you may find it more compact to write your own routines that suit your specific needs, for example:
outInt (char *buff, int intVal); outChr (char *buff, char chVal); outStr (char *buff, char *strVal);
etc., for writing to buffers, then outBuff (char *buff) to send it to a file or standard output.
For example, if you control the data used (without line overflows, 16-bit binary additions, etc.), you can use the following functions:
#include <stdio.h> #include <stdlib.h> #include <string.h> void outChr (char *buff, char chVal) { *buff++ = chVal; *buff = '\0'; } void outStr (char *buff, char *strVal) { strcpy (buff, strVal); }
void outInt (char *buff, int intVal) { int divisor = 10000, printing = 0; // Special cases. if (intVal == -32768) { outStr (buff, "-32768"); return; } if (intVal == 0) { outChr (buff, '0'); return; } // Handle negatives. if (intVal < 0) { outChr (buff++, '-'); intVal = -intVal; } // Handle non-zero positives <= 32767. while (divisor > 0) { if ((intVal >= divisor) || printing) { outChr (buff++, "0123456789"[intVal/divisor]); printing = 1; } intVal = intVal % divisor; divisor /= 10; } }
int main (int argc, char *argv[]) { char buff[1000]; int i; for (i = 1; i < argc; i++) { outInt (buff, atoi (argv[i])); printf ("[%s] -> [%s]\n", argv[i], buff); } return 0; }
Launch with:
pax$ tstprg 32767 10000 9999 10 9 1 0 -1 -9 -10 -99 -10000 -32767 -32768
outputs:
[32767] -> [32767] [10000] -> [10000] [9999] -> [9999] [10] -> [10] [9] -> [9] [1] -> [1] [0] -> [0] [-1] -> [-1] [-9] -> [-9] [-10] -> [-10] [-99] -> [-99] [-10000] -> [-10000] [-32767] -> [-32767] [-32768] -> [-32768]
These functions should be relatively small in size, as they focus on specific needs, rather than the much more general printf family.