From 1509ed785a0bc06e87ea087c46cc77a1eb700960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20-rsm-=20Marek?= Date: Thu, 18 Jul 2024 17:54:41 +0200 Subject: [PATCH] Exerc 7_3. --- chapter_7.input_output/7_3.minprintf/Makefile | 3 ++ chapter_7.input_output/7_3.minprintf/main.c | 10 ++++++ .../7_3.minprintf/minprintf.c | 31 +++++++++++++++++++ .../7_3.minprintf/minprintf.h | 7 +++++ .../7_3.minprintf/tests/0_internal.tin | 0 .../7_3.minprintf/tests/0_internal.tout | 6 ++++ 6 files changed, 57 insertions(+) create mode 100644 chapter_7.input_output/7_3.minprintf/Makefile create mode 100644 chapter_7.input_output/7_3.minprintf/main.c create mode 100644 chapter_7.input_output/7_3.minprintf/minprintf.c create mode 100644 chapter_7.input_output/7_3.minprintf/minprintf.h create mode 100644 chapter_7.input_output/7_3.minprintf/tests/0_internal.tin create mode 100644 chapter_7.input_output/7_3.minprintf/tests/0_internal.tout diff --git a/chapter_7.input_output/7_3.minprintf/Makefile b/chapter_7.input_output/7_3.minprintf/Makefile new file mode 100644 index 0000000..e230801 --- /dev/null +++ b/chapter_7.input_output/7_3.minprintf/Makefile @@ -0,0 +1,3 @@ +BINARY=minprintf + +include ../../Exercise_incl.mk diff --git a/chapter_7.input_output/7_3.minprintf/main.c b/chapter_7.input_output/7_3.minprintf/main.c new file mode 100644 index 0000000..fac74ca --- /dev/null +++ b/chapter_7.input_output/7_3.minprintf/main.c @@ -0,0 +1,10 @@ +# include "minprintf.h" + +int main () +{ + minprintf("int -100: %d\nuint 222: %u\nhex 1A: %x\ndouble -5.003: %f\nchar 'X': %c\n" + "str \"KnR Solutions\": %s\n", + (int) -100, (unsigned int) 222, (int) 26, (double) -5.003, (char) 'X', + "KnR Solutions"); + return 0; +} diff --git a/chapter_7.input_output/7_3.minprintf/minprintf.c b/chapter_7.input_output/7_3.minprintf/minprintf.c new file mode 100644 index 0000000..b0c28b6 --- /dev/null +++ b/chapter_7.input_output/7_3.minprintf/minprintf.c @@ -0,0 +1,31 @@ +# include +# include + +/* minprintf: minimal printf with variable argument list */ +void minprintf (char *fmt, ...) +{ + va_list ap; /* points to each unnamed arg in turn */ + char *p, *sval; + + va_start(ap, fmt); /* make ap point to 1st unnamed arg */ + for (p = fmt; *p; p++) { + if (*p != '%') { + putchar(*p); + continue; + } + switch (*++p) { + case 'i': + case 'd': printf("%d", va_arg(ap, int)); break; + case 'u': printf("%u", va_arg(ap, unsigned int)); break; + case 'x': printf("%x", va_arg(ap, int)); break; + case 'f': printf("%f", va_arg(ap, double)); break; + case 'c': printf("%c", va_arg(ap, int)); break; + case 's': + for (sval = va_arg(ap, char *); *sval; sval++) + putchar(*sval); + break; + default: putchar(*p); break; + } + } + va_end(ap); /* clean up when done */ +} diff --git a/chapter_7.input_output/7_3.minprintf/minprintf.h b/chapter_7.input_output/7_3.minprintf/minprintf.h new file mode 100644 index 0000000..1aa98fa --- /dev/null +++ b/chapter_7.input_output/7_3.minprintf/minprintf.h @@ -0,0 +1,7 @@ +# ifndef MINPRINTF_H +# define MINPRINTF_H + +/* minprintf: minimal printf with variable argument list */ +void minprintf (char *fmt, ...); + +# endif diff --git a/chapter_7.input_output/7_3.minprintf/tests/0_internal.tin b/chapter_7.input_output/7_3.minprintf/tests/0_internal.tin new file mode 100644 index 0000000..e69de29 diff --git a/chapter_7.input_output/7_3.minprintf/tests/0_internal.tout b/chapter_7.input_output/7_3.minprintf/tests/0_internal.tout new file mode 100644 index 0000000..3645af0 --- /dev/null +++ b/chapter_7.input_output/7_3.minprintf/tests/0_internal.tout @@ -0,0 +1,6 @@ +int -100: -100 +uint 222: 222 +hex 1A: 1a +double -5.003: -5.003000 +char 'X': X +str "KnR Solutions": KnR Solutions