forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inttypes_h.c
78 lines (53 loc) · 1.71 KB
/
inttypes_h.c
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
/*
# PRIxPTR
*/
#include "common.h"
int main() {
#if __STDC_VERSION__ >= 199901L
/*
# PRIxPTR
0 pad pointers.
To print pointers and line them up nicely,
one must take into account that trailing zeroes are omitted.
One option is to space pad:
%10
But this produces:
0x10
0x10000000
which is still ugly.
The ideal would then be to pad with zeros as in:
0x00000010
0x10000000
The notation:
%010p
is not supported TODO why not?
The solution to this introduced in C99 is to use `uintptr + PRIxPTR`:
http://stackoverflow.com/questions/1255099/whats-the-proper-use-of-printf-to-display-pointers-padded-with-0s
There seems to be no convenient way to take into account pointer sizes except defining thingg manually:
For example, x32 uses 4 bytes, x64 8, etc.
*/
{
/* 2 for "0x" and one for trailing '\0'. */
char s[PRIxPTR_WIDTH + 3];
printf("PRIxPTR = %s\n", PRIxPTR);
printf("PRIxPTR usage = %0*" PRIxPTR "\n", PRIxPTR_WIDTH, (uintptr_t)(void*)1);
}
/*
# Fixed size integer printf format
# PRId16
# PRId32
# PRId64
Why not modify printf instead of adding those new macros?
http://stackoverflow.com/questions/1183679/why-werent-new-bit-width-specific-printf-format-option-strings-adoped-as-pa
# PRIu32
Unsigned versions.
*/
{
printf("PRId16 = %s\n", PRId16);
printf("PRId32 = %s\n", PRId32);
printf("PRId64 = %s\n", PRId64);
/* Samplel usage:*/
printf("%" PRIx32 "\n", (uint32_t)0xFFFFFFFF);
}
#endif
}