sprintf实例

例子1:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
    /* -------------------------------------- */
    char buf[30] = {0};
    sprintf(buf, "%8X" , 123);
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%8X" , 100);
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%8X" , 84);
    printf("%s\n", buf);

    /* -------------------------------------- */
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%8x" , 123);
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%8x" , 100);
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%8x" , 84);
    printf("%s\n", buf);

  /* -------------------------------------- */
   
  memset(buf,
0, sizeof(buf));
   
  sprintf(buf,
"%-8x" , 123);
   
  printf(
"%s\n", buf);


  memset(buf,
0, sizeof(buf));
   
  sprintf(buf,
"%-8x" , 100);
   
  printf(
"%s\n", buf);



  memset(buf,
0, sizeof(buf));
   
  sprintf(buf,
"%-8x" , 84);
   
  printf(
"%s\n", buf);
return 0; }

程序输出:

[root@localhost ~]# ./a.out
      7B
      64
      54
      7b
      64
      54
7b
64
54
[root@localhost ~]#

(1)%X表示大写16进制,%x表示小写16进制;

(2)%8X表示“右对齐”,即串总共占8位,不足补空格;

(2)%-8X表示“左对齐”,即串总共占8位,不足补空格;

 

例子2:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
    char buf[30];

    /* -------------------------------------- */
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%4x" , 123123);
    printf("%s\n", buf);  // 原样输出

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%4x" , 10000);
    printf("%s\n", buf);  // 原样输出

    /* -------------------------------------- */
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%-4x" , 123123);
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%-4x" , 10000);
    printf("%s\n", buf);

    return 0;
}

程序输出:

[root@localhost ~]# ./a.out
1e0f3
2710
1e0f3
2710
[root@localhost ~]#

(1)当串的宽度超过或等于,格式说明符所指定的宽度时,格式说明符不起作用,串原样输出。

 

例子3:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
    char buf[30];

    /* -------------------------------------- */
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%08x" , 123123);
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%08x" , 10000);
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%08x" , 84);
    printf("%s\n", buf);

    /* -------------------------------------- */
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%@8x" , 123123);
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%@8x" , 10000);
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%@8x" , 84);
    printf("%s\n", buf);

    return 0;
}

程序输出:

[root@localhost ~]# ./a.out
0001e0f3
00002710
00000054
%@8x
%@8x
%@8x
[root@localhost ~]#

(1)这里将左边补0,如果补其他的就不行了。

 

例子4:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
    char buf[30];

    /* -------------------------------------- */
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%5.3s" , "ZhuHaiXiangZhou");
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%8.8s" , "ZhuHaiXiangZhou");
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%3.8s" , "ZhuHaiXiangZhou");  // 当小数部分大于整数部分的时候,忽略整数部分
    printf("%s\n", buf);

    /* -------------------------------------- */
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%.5s" , "ZhuHaiXiangZhou");
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%.3s" , "ZhuHaiXiangZhou");
    printf("%s\n", buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%.7s" , "ZhuHaiXiangZhou");
    printf("%s\n", buf);

    return 0;
}

程序输出:

[root@localhost ~]# ./a.out
  Zhu
ZhuHaiXi
ZhuHaiXi
ZhuHa
Zhu
ZhuHaiX
[root@localhost ~]#

(1)在”%m.ns”中,m表示占用宽度(字符串长度不足时补空格,
超出了则按照实际宽度打印),n才表示从相应的字符串中最多取用的字符数。通常在打印字符串时m
没什么大用,还是点号后面的n用的多。 当小数部分大于整数部分的时候,忽略整数部分,如这里的"%3.8s"。

posted @ 2013-05-07 22:17  robotke1  阅读(654)  评论(0编辑  收藏  举报