Reputation: 149
char buf[10];
int counter, x = 0;
snprintf (buf, sizeof buf , "%.100d%n", x, &counter);
printf("Counter: %d\n", counter)
I am learning about precision with printf. With %.100d%n, the precision here gives 100 digits for rendering x.
What I don't understand is why would the counter be incremented to 100, although only 10 characters are actually written to the buffer?
Upvotes: 1
Views: 230
Reputation: 108938
The ten bytes written to buf
are 9 spaces and 1 '\0'
(zero terminator), counter
is assigned the value 100
for 99 spaces and 1 '0'
(digit zero).
buf <== " "
%.100d <== " ..... 0"
Note that buf
is incomplete: it does not have a '0'
.
Upvotes: 1