-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24e262a
commit 1509ed7
Showing
6 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
BINARY=minprintf | ||
|
||
include ../../Exercise_incl.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |