That's my choice any day of the week but I didn't know how much OP wants to learn a C function
I visit this site every time I forget how to use all the nice features of sprintf:
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
If you do "%3d" then you get space padding to the left.