Web lists-archives.com

[Mingw-users] snprintf buggy in gcc 4.9.3-1

Using the conversion %02d or %2d should produce a string of (at 
least) 2 characters.
However when the number to be printed has only one digit snprintf 
produces a field width of 1
A workaround is to use _snprintf.

gcc.exe (GCC) 4.9.3-1 as obtained by mingw-get on juli 30 2016
windows-10 64-bit

Regards, Peter

#include <stdio.h>
#include <string.h>
int main()
   char string[8];

//snprintf() drops the leading 0 in mingw gcc 4.9.3-1 !!
   printf("snprintf:\tstring=%s len=%d\n",string,strlen(string));

// with _snprintf() no problem
   printf("_snprintf:\tstring=%s len=%d\n",string,strlen(string));

   return 0;
Program output

snprintf:       string=3 len=1
_snprintf:      string=03 len=2

MinGW-users mailing list

This list observes the Etiquette found at 
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

You may change your MinGW Account Options or unsubscribe at:
Also: mailto:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe