请教g++编译器如何格式化输出指定宽度的字符串,不足的补0

请问g++编译器怎么格式化输出指定宽度的字符串,不足的补0
char a[10] = {0};
sprintf(a, "%03s", "12");
printf("%s", a);

在vs的编译后输出是:"012"
在g++编译后输出是:" 12"

难道说在g++下就不能格式化的输出不足位数补零的需求了吗?
------解决方案--------------------
我在windows上用MinGW g++编译能输出0,但是在Linux下的确不行
------解决方案--------------------
引用:
求高手解答是不是g++编译器,本身的缺陷啊。。

不是,%后面flag 0不对%s,只对应数字。 请教g++编译器如何格式化输出指定宽度的字符串,不足的补0
------解决方案--------------------
引用:
char a[10] = {0};
sprintf(a, "%03s", "12");
printf("%s", a);

在vs的编译后输出是:"012"
在g++编译后输出是:" 12"

难道说在g++下就不能格式化的输出不足位数补零的需求了吗?

自己填充嘛

#include <stdio.h>

int
main(int argc, char *argv[])
{
    char *p;
    char a[10] = {0};

    sprintf(a, "%3s", "12");
    for (p = a; *p == ' '; ++p) {
        *p = '0';
    }
    printf("%s\n", a);

    return 0;
}

------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

求高手解答是不是g++编译器,本身的缺陷啊。。




       0      The value should be zero padded.  For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, the con-
              verted value is padded on the left with zeros rather than blanks.  If the 0 and - flags both appear, the
              0 flag is ignored.  If a precision is given with a numeric conversion (d, i, o, u, x, and X), the 0 flag
              is ignored.  For other conversions, the behavior is undefined.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
int main() {
    int i,v;
    char bs[33];
    char b[33];
    char hs[9];
    char h[9];
    char s[4];
    char *e;

// 十进制整数转二进制串;
    i=1024;
    ltoa(i,b,2);
    sprintf(bs,"%032s",b);
    printf("i=%d,bs=%s\n",i,bs);
// 十进制整数转十六进制串;
    i=1024;
    ltoa(i,h,16);
    sprintf(hs,"%08s",h);
    printf("i=%d,hs=%s\n",i,hs);
// 十六进制字符串转成十进制数
    strcpy(hs,"00000400");
    sscanf(hs,"%x",&i);
    printf("hs=%s,i=%d\n",hs,i);
// 二进制字符串转化为十六进制字符串;
    strcpy(bs,"00000000000000000000010000000000");
    i=strtol(bs,&e,2);
    ltoa(i,h,16);
    sprintf(hs,"%08s",h);
    printf("bs=%s,hs=%s\n",bs,hs);
// 二进制字符串转化为十进制数;
    strcpy(bs,"00000000000000000000010000000000");
    i=strtol(bs,&e,2);
    printf("bs=%s,i=%d\n",bs,i);
// 十六进制字符串转成二进制串
    strcpy(hs,"00000400");
    sscanf(hs,"%x",&i);
    ltoa(i,b,2);
    sprintf(bs,"%032s",b);
    printf("hs=%s,bs=%s\n",hs,bs);
// ASC\GBK字符串转十六进制串
    strcpy(s,"a汉");
    i=0;
    while (1) {
        if (0==s[i]) break;
        sprintf(hs+i*2,"%02X",(unsigned char)s[i]);
        i++;
    }
    setlocale(LC_ALL,"chs");
    printf("s=%s,hs=%s\n",s,hs);
// 十六进制字符串转成汉字(GBK)及字符(ASC)
    strcpy(hs,"61BABA");
    i=0;
    while (1) {
        if (1!=sscanf(hs+i*2,"%2x",&v)) break;
        s[i]=(char)v;
        i++;
    }