Browse Source

libc: support not printing 0 digit with precision of 0

K. Lange 7 months ago
parent
commit
cc8b933cda
2 changed files with 14 additions and 1 deletions
  1. 11 0
      apps/test-printf.c
  2. 3 1
      libc/stdio/printf.c

+ 11 - 0
apps/test-printf.c

@@ -0,0 +1,11 @@
+#include <stdio.h>
+
+int main(int argc, char * argv[]) {
+	printf("%.3d\n", 42);
+	printf("%.10d\n", 12345);
+	printf("%.1d\n", 0);
+	printf("%.0d\n", 0);
+	printf("%.0d\n", 1);
+	printf("%.0d\n", 123);
+	return 0;
+}

+ 3 - 1
libc/stdio/printf.c

@@ -7,7 +7,9 @@ static void print_dec(unsigned int value, unsigned int width, char * buf, int *
 	unsigned int i = 9;
 	if (precision == -1) precision = 1;
 
-	if (value < 10UL) {
+	if (value == 0) {
+		n_width = 0;
+	} else if (value < 10UL) {
 		n_width = 1;
 	} else if (value < 100UL) {
 		n_width = 2;