Skip to content

Commit f64b8df

Browse files
committed
7.1 -> 7.3
1 parent cb7c5f2 commit f64b8df

File tree

3 files changed

+81
-14
lines changed

3 files changed

+81
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <ctype.h>
2+
#include <stdarg.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
/* minprintf: minimal printf with field width and precision */
7+
void minprintf(char* fmt, ...)
8+
{
9+
va_list ap;
10+
char *p, *sval;
11+
int ival;
12+
double dval;
13+
14+
va_start(ap, fmt);
15+
for (p = fmt; *p; p++)
16+
{
17+
if (*p != '%')
18+
{
19+
putchar(*p);
20+
continue;
21+
}
22+
23+
// Start parsing format
24+
p++; // skip '%'
25+
26+
// Parse optional width and precision
27+
char format[32] = "%";
28+
int i = 1;
29+
30+
while (isdigit(*p) || *p == '.' || *p == '-')
31+
{
32+
if (i < sizeof(format) - 1)
33+
format[i++] = *p++;
34+
}
35+
36+
format[i++] = *p; // type specifier like d, f, s
37+
format[i] = '\0';
38+
39+
switch (*p)
40+
{
41+
case 'd':
42+
ival = va_arg(ap, int);
43+
printf(format, ival);
44+
break;
45+
case 'f':
46+
dval = va_arg(ap, double);
47+
printf(format, dval);
48+
break;
49+
case 's':
50+
sval = va_arg(ap, char*);
51+
printf(format, sval);
52+
break;
53+
default:
54+
// Unrecognized specifier
55+
putchar(*p);
56+
break;
57+
}
58+
}
59+
va_end(ap);
60+
}
61+
62+
int main(void)
63+
{
64+
minprintf("%.5d", 123);
65+
return EXIT_SUCCESS;
66+
}

tasks/Chapter-7:Input-Output/7-3.md

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11

2-
# Exercise 7-3
2+
## Exercise 7-3 — The C Programming Language, 2nd Edition
33

4-
## Task
4+
Revise the `minprintf` function from Chapter 7 to support more of the formatting capabilities of the standard `printf` function. The original `minprintf` handles only a limited set of conversion specifiers (like `%d`, `%f`, `%s`, etc.) and minimal flag support.
55

6-
Revise `minprintf` to handle more of the other facilities of `printf`.
6+
Your task is to improve it so it can interpret and pass along additional format specifiers and flags, such as:
77

8-
## Objective
8+
- Field width and precision (e.g., `%6.2f`)
9+
- Left-justification (`%-d`)
10+
- Zero-padding (`%05d`)
11+
- Alternate form (`%#x`, `%#o`)
12+
- Signs (`%+d`, `% d`)
913

10-
Extend the functionality of the `minprintf` function from Chapter 7 of *The C Programming Language (K&R)* to support more format specifiers and flags, closer to the standard `printf` behavior.
14+
### Notes
1115

12-
## Requirements
16+
- The goal is not to reimplement `printf` but to parse the format string more fully and correctly pass the format and corresponding argument to `printf`.
17+
- Use <stdarg.h> for handling the variable arguments, as shown in the original `minprintf`.
18+
- Test with a variety of format strings to ensure your implementation works correctly.
1319

14-
- Add support for additional format specifiers such as:
15-
- `%o`, `%x`, `%X`, `%u`, `%c`, `%e`, `%g`, etc.
16-
- Handle flags like:
17-
- `-` (left justify), `+` (sign), `0` (zero-padding), `#` (alternate form), and space.
18-
- Optionally support field width and precision (e.g., `%10.2f`).
20+
### Deliverables
1921

20-
## Notes
21-
22-
This task is meant to deepen understanding of variadic functions, format parsing, and output formatting in C.
22+
- An updated `minprintf` function in C
23+
- Example usage that demonstrates the supported features

0 commit comments

Comments
 (0)