#define
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//sprintf格式化字符串
void test01()
{
char buf[1024];
memset(buf,0,1024);
sprintf(buf,"今天是%d年%d月%d日\n",2018,5,6);
printf("%s",buf);
//字符串拼接
memset(buf,0,1024);
char str1[] = "hell";
char str2[] = "world";
int len = sprintf(buf,"%s%s", str1, str2);//sprintf返回的是字符串的长度
printf("buf:%s len:%d\n",buf,len);
//数字转字符串
memset(buf,0,1024);
int num = 100;
sprintf(buf,"%d",num);
printf("buf:%s\n",buf);
//设置宽度 右对齐
memset(buf,0,1024);
sprintf(buf,"%8d",num);
printf("buf:%s\n",buf);
//设置宽度 左对齐
memset(buf,0,1024);
sprintf(buf,"%-8d",num);
printf("buf:%s\n",buf);
//转成16进制字符串 小写
memset(buf,0,1024);
sprintf(buf,"0x%x",num);
printf("buf:%s\n",buf);
//转成8进制字符串
memset(buf,0,1024);
sprintf(buf,"0%o",num);
printf("buf:%s\n",buf);
}
int main()
{
test01();
return EXIT_SUCCESS;
}