Skip to content

Commit

Permalink
Exerc 7_3.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsm-lisper committed Jul 18, 2024
1 parent 24e262a commit 1509ed7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chapter_7.input_output/7_3.minprintf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BINARY=minprintf

include ../../Exercise_incl.mk
10 changes: 10 additions & 0 deletions chapter_7.input_output/7_3.minprintf/main.c
Original file line number Diff line number Diff line change
@@ -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;
}
31 changes: 31 additions & 0 deletions chapter_7.input_output/7_3.minprintf/minprintf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# include <stdio.h>
# include <stdarg.h>

/* 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 */
}
7 changes: 7 additions & 0 deletions chapter_7.input_output/7_3.minprintf/minprintf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ifndef MINPRINTF_H
# define MINPRINTF_H

/* minprintf: minimal printf with variable argument list */
void minprintf (char *fmt, ...);

# endif
Empty file.
6 changes: 6 additions & 0 deletions chapter_7.input_output/7_3.minprintf/tests/0_internal.tout
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1509ed7

Please sign in to comment.