-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_printf_v3.c
More file actions
145 lines (124 loc) · 3.43 KB
/
simple_printf_v3.c
File metadata and controls
145 lines (124 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
/*
* Copyright 2023, J. Zbiciak <joe.zbiciak@leftturnonly.info>
* Author: Joe Zbiciak <joe.zbiciak@leftturnonly.info>
* SPDX-License-Identifier: CC-BY-SA-4.0
*/
/* Prints an unsigned decimal integer to stdout. */
void print_unsigned_decimal_int(unsigned u) {
/* Inefficient, but portable. */
unsigned pow10 = 1;
unsigned tmp = u;
while (tmp >= 10) {
pow10 *= 10;
tmp /= 10;
}
/* Now print the decimalized value. */
while (pow10 > 0) {
putchar('0' + u / pow10);
u %= pow10;
pow10 /= 10;
}
}
/* Prints a signed decimal integer to stdout. */
void print_signed_decimal_int(int i) {
unsigned u = i;
/*
* Handle negative numbers. This might look odd, but it avoids undefined
* behavior for the largest negative number by negating the _unsigned_ value
* after testing the sign of the signed value.
*/
if (i < 0) {
putchar('-');
u = -u;
}
print_unsigned_decimal_int(u);
}
/* Prints a hexadecimal integer to stdout. */
void print_hexadecimal_int(unsigned u, bool caps) {
/* Inefficient, but portable. */
unsigned pow16 = 1;
unsigned tmp = u;
while (tmp >= 16) {
pow16 *= 16;
tmp /= 16;
}
/* Now print the decimalized value. */
const char *hex_digits = caps ?"0123456789ABCDEF" : "0123456789abcdef";
while (pow16 > 0) {
putchar(hex_digits[u / pow16]);
u %= pow16;
pow16 /= 16;
}
}
/* Simplified printf that only understands %s, %d, %i, %u, %x, %X and %%. */
void simple_printf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
for (int ch = *fmt++; ch; ch = *fmt++) {
/* If it's not %, just print the character. */
if (ch != '%') {
fputc(ch, stdout);
continue;
}
/* It's (potentially) a conversion. Let's take a look. */
int conv = *fmt++;
switch (conv) {
case 's': {
/* %s is a string. */
const char *s = va_arg(args, const char *);
fputs(s, stdout);
break;
}
case 'i':
case 'd': {
/* %d and %i are signed integers. */
int i = va_arg(args, int);
print_signed_decimal_int(i);
break;
}
case 'u': {
/* %u is an unsigned integer. */
unsigned u = va_arg(args, unsigned);
print_unsigned_decimal_int(u);
break;
}
case 'x':
case 'X': {
/* %x and %X are hexadecimal integers. */
unsigned u = va_arg(args, unsigned);
print_hexadecimal_int(u, conv == 'X');
break;
}
case '%': {
/* %% prints '%' */
putchar('%');
break;
}
default: {
/* Not a valid conversion. Print the '%' and back up. */
putchar('%');
--fmt;
break;
}
}
}
va_end(args);
}
int main() {
simple_printf("Hello %s, the answer is %d.\n", "world", 42);
simple_printf("Zero: %d\n", 0);
simple_printf("Positive %%d: %d\n", 123456789);
simple_printf("Negative %%d: %d\n", -123456789);
simple_printf("Positive %%i: %i\n", 123456789);
simple_printf("Negative %%i: %i\n", -123456789);
simple_printf("Unsigned %%u: %u\n", 4000000000U);
simple_printf("Hex %%x: %x\n", 123456789);
simple_printf("Hex %%x: %x\n", -123456789);
simple_printf("Hex %%x: %x\n", 4000000000U);
simple_printf("Hex %%X: %X\n", 123456789);
simple_printf("Hex %%X: %X\n", -123456789);
simple_printf("Hex %%X: %X\n", 4000000000U);
}