From a8db504e7d098123075bd00d2a998488a97af81d Mon Sep 17 00:00:00 2001 From: zlg Date: Sun, 26 Jun 2016 02:57:18 -0700 Subject: Solve Exercise 7-3: minprintf() --- ch7/7-03_minprintf.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 ch7/7-03_minprintf.c (limited to 'ch7') diff --git a/ch7/7-03_minprintf.c b/ch7/7-03_minprintf.c new file mode 100644 index 0000000..92cbde1 --- /dev/null +++ b/ch7/7-03_minprintf.c @@ -0,0 +1,75 @@ +#include +#include + +/* The C Programming Language: 2nd Edition + * + * Exercise 7-3: Revise `minprintf` to handle more of the other facilities + * of `printf`. + * + * Notes: It doesn't specify *which* facilities, or how many, so we'll choose + * three: + * + * o - octal + * x - hexadecimal + * c - unsigned char + * + * Since we're passing to printf(), this was easy. Doing it ourselves would + * take some more work. But we'll skirt by with the minimum. + */ + +void minprintf(char *fmt, ...) { + va_list ap; + char *p, *sval; + int ival; + double dval; + va_start(ap, fmt); + for (p = fmt; *p; p++) { + if (*p != '%') { + putchar(*p); + continue; + } + switch (*++p) { + case 'd': + ival = va_arg(ap, int); + printf("%d", ival); + break; + case 'f': + dval = va_arg(ap, double); + printf("%f", dval); + break; + case 's': + for (sval = va_arg(ap, char *); *sval; sval++) { + putchar(*sval); + } + break; + case 'c': + ival = va_arg(ap, int); + printf("%c", ival); + break; + case 'x': + ival = va_arg(ap, int); + printf("0x"); + printf("%x", ival); + break; + case 'o': + ival = va_arg(ap, int); + printf("0o"); + printf("%o", ival); + break; + default: + putchar(*p); + break; + } + } + va_end(ap); +} + +int main(int argc, char *argv[]) { + /* Should output 0o144 */ + minprintf("100 = %o\n", 100); + /* Should output 0x64 */ + minprintf("100 = %x\n", 100); + /* Should output a semicolon */ + minprintf("char(0x59) = %c\n", 59); + return 0; +} -- cgit v1.2.3-54-g00ecf