int main (void) { int slen ; char *ptr = "Hello world!"; char *np = 0; char buf[128]; termf ("ptr=%s, %s is null pointer, char %c='a'\n", ptr, np, 'a'); termf ("hex %x = ff, hex02=%02x\n", 0xff, 0); // hex handling termf ("signed %d = %uU = 0x%X\n", -3, -3, -3); // int formats termf ("%d %s(s) with %%\n", 0, "message"); slen = stringf (buf, "justif: left=\"%-10s\", right=\"%10s\"\n", "left", "right"); termf ("[%d] %s", slen, buf); stringf(buf, "padding (pos): zero=[%04d], left=[%-4d], r=[%4d]\n", 3, 3, 3) ; termf ("%s", buf); // test walking string builder slen = 0 ; slen += stringf(buf+slen, "padding (neg): zero=[%04d], ", -3) ; slen += stringf(buf+slen, "left=[%-4d], ", -3) ; slen += stringf(buf+slen, "right=[%4d]\n", -3) ; termf ("[%d] %s", slen, buf); termf("+ format: int: %+d, %+d, double: %+.1f, %+.1f, reset: %d, %.1f\n", 3, -3, 3.0, -3.0, 3, 3.0) ; stringf (buf, "%.2f is a double\n", 3.31) ; termf ("%s", buf); stringf (buf, "multiple unsigneds: %u %u %2u %X\n", 15, 5, 23, 0xB38F) ; termf ("%s", buf); stringf (buf, "multiple chars: %c %c %c %c\n", 'a', 'b', 'c', 'd') ; termf ("%s", buf); stringf (buf, "multiple doubles: %f %.1f %2.0f %.2f %.3f %.2f [%-8.3f]\n", 3.45, 3.93, 2.45, -1.1, 3.093, 13.72, -4.382) ; termf ("%s", buf); stringf (buf, "double special cases: %f %.f %.0f %2f %2.f %2.0f\n", 3.14159, 3.14159, 3.14159, 3.14159, 3.14159, 3.14159) ; termf ("%s", buf); stringf (buf, "rounding doubles: %.1f %.1f %.3f %.2f [%-8.3f]\n", 3.93, 3.96, 3.0988, 3.999, -4.382) ; termf ("%s", buf); // test case from our Yagarto ARM9 problem // char *cmd = "adc_vref " ; // float fvalue = 3.30 ; // stringf (buf, "%s%.2f", cmd, (double) fvalue); // printf("%s\n", buf); return 0; }
int main (void) { int slen ; char *ptr = "Hello world!"; char *np = 0; char buf[128]; termf ("ptr=%s, %s is null pointer, char %c='a'\n", ptr, np, 'a'); termf ("hex %x = ff, hex02=%02x\n", 0xff, 0); // hex handling termf ("signed %d = unsigned %u = hex %X\n", -3, -3, -3); // int formats termf ("%d %s(s) with %%\n", 0, "message"); slen = stringf (buf, "justif: left=\"%-10s\", right=\"%10s\"\n", "left", "right"); termf ("[%d] %s", slen, buf); stringf(buf, "padding (pos): zero=[%04d], left=[%-4d], r=[%4d]\n", 3, 3, 3) ; termf ("%s", buf); // test walking string builder slen = 0 ; slen += stringf(buf+slen, "padding (neg): zero=[%04d], ", -3) ; slen += stringf(buf+slen, "left=[%-4d], ", -3) ; slen += stringf(buf+slen, "right=[%4d]\n", -3) ; termf ("[%d] %s", slen, buf); stringf (buf, "%.2f is a double\n", 3.31) ; termf ("%s", buf); stringf (buf, "multiple unsigneds: %u %u %2u %X\n", 15, 5, 23, 0xB38F) ; termf ("%s", buf); stringf (buf, "multiple chars: %c %c %c %c\n", 'a', 'b', 'c', 'd') ; termf ("%s", buf); stringf (buf, "multiple doubles: %.2f %2.0f %.2f %.3f %.2f [%-8.3f]\n", 3.31, 2.45, -1.1, 3.093, 13.72, -4.382) ; termf ("%s", buf); return 0; }