I\'m a novice programmer, but usually I can unravel my own issues. This time I solved the issue, but it still stumps me. A friend suggested I ask this community for input.>
You're forgetting the NUL terminator. In C, strings require an extra character for the terminator, so char buf[2]
ought to be char buf[3]
to accommodate numbers between 10 and 99.
Incidentally, your code demonstrates why sprintf
is dangerous as it can write past the output buffer and enable stack smashing attacks. A better options is to use snprintf
.