Linux: printing a zero-padded string of specified length

Viewed 662

On Linux, I am using GNU gcc 4.9.2 version and getting a strange unexpected behavior while trying to print a zero-padded string of specified length. Here's the code snippet I am trying:

#include <cstdio>
#include <cstring>
int main()
{
  char buff[5];
  sprintf(buff,"%04s","12");
  printf("%s\n", buff);
  return 0;
}

While the documentation given in http://www.cplusplus.com/reference/cstdio/printf/ clearly states that flag left-pads the number with zeroes (0) instead of spaces when padding is specified. But, it's printing space padded "12" i.e. " 12" and not "0012". remedy?

2 Answers
Related