Skip to content

Commit 17fad7d

Browse files
committed
Fixed printf of long and unsigned types in hexadecimal
"%x" format specifier was ignoring preceding "l" or "ll" specifiers, and casting everything to int (rather than unsigned).
1 parent c04e46c commit 17fad7d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

c-tests/long.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
3+
int main(void) {
4+
unsigned long a = 1;
5+
6+
printf("%2d: 0x%016lx\n", 0, a);
7+
8+
for (int i = 1; i < 65; i++) {
9+
a <<= 1;
10+
printf("%2d: 0x%016llx\n", i, a);
11+
}
12+
13+
return 0;
14+
}

cstdlib/stdio.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,15 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut,
282282
case 'o':
283283
case 'x':
284284
case 'X':
285-
ShowType = &pc->IntType;
285+
if (ShowLong == 1) {
286+
ShowLong = 0;
287+
ShowType = &pc->UnsignedLongType;
288+
} else if (ShowLong > 1) {
289+
ShowLong = 0;
290+
ShowType = &pc->UnsignedLongLongType;
291+
} else {
292+
ShowType = &pc->UnsignedIntType;
293+
}
286294
break; /* integer base conversions */
287295
case 'l':
288296
ShowLong++;

0 commit comments

Comments
 (0)