sprintf 用法 注意事项
来源:互联网 发布:自己室内设计软件 编辑:程序博客网 时间:2024/05/06 07:28
源:http://hi.baidu.com/woxlinoble/blog/item/b02a8a82fd795190f603a6d2.html
sprintf是个变参函数,定义如下:
1. 格式化数字字符串
- //把整数123打印成一个字符串保存在s中。sprintf(s, "%d", 123); //产生"123"
- 可以指定宽度,不足的左边补空格:sprintf(s, "%8d%8d", 123, 4567); //产生:" 123 4567"
- 当然也可以左对齐:sprintf(s, "%-8d%8d", 123, 4567); //产生:"123 4567"
- 也可以按照16进制打印:sprintf(s, "%8x", 4567); //小写16进制,宽度占8个位置,右对齐
- sprintf(s, "%-8X", 4568); //大写16进制,宽度占8个位置,左对齐。这样,一个整数的16进制字符串就很容易得到,但我们在打印16进制内容时,通常想要一种左边补0的等宽格式,那该怎么做呢?很简单,在表示宽度的数字前面加个0就可以了。
- sprintf(s, "%08X", 4567); //产生:"000011D7"
short si = -1;
sprintf(s, "%04X", si);
sprintf(s, "%04X", (unsigned short)si);
unsigned short si = -1;
sprintf(s, "%04X", si);
sprintf(s, "%10.3f", 3.1415626); //产生:" 3.142"
sprintf(s, "%-10.3f", 3.1415626); //产生:"3.142 "
sprintf(s, "%.3f", 3.1415626); //不指定总宽度,产生:"3.142"
int i = 100;
sprintf(s, "%.2f", i);
sprintf(s, "%.2f", (double)i);
for(int i = 32; i < 127; i++) {
printf("[ %c ]: %3d 0x%#04X/n", i, i, i);
}
char* who = "I";
char* whom = "CSDN";
sprintf(s, "%s love %s.", who, whom); //产生:"I love CSDN. "
char a1[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
char a2[] = {'H', 'I', 'J', 'K', 'L', 'M', 'N'};
sprintf(s, "%.6s%.5s", a1, a2);//产生:"ABCDEFHIJKL"
sprintf(s, "%.*s%.*s", 7, a1, 7, a2);
sprintf(s, "%.*s%.*s", sizeof(a1), a1, sizeof(a2), a2);
sprintf(s, "%-*d", 4, 'A'); //产生"65 "
sprintf(s, "%#0*X", 8, 128); //产生"0X000080","#"产生0X
sprintf(s, "%*.*f", 10, 2, 3.1415926); //产生" 3.14"
sprintf(s, "%u", &i);
sprintf(s, "%08X", &i);
sprintf(s, "%p", &i);
我觉得它实际上就相当于:
sprintf(s, "%0*x", 2 * sizeof(void *), &i);
int len = sprintf(s, "%d", i);对于正整数来说,len便等于整数i的10进制位数。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main() {
srand(time(0));
char s[64];
int offset = 0;
for(int i = 0; i < 10; i++) {
offset += sprintf(s + offset, "%d,", rand() % 100);
}
s[offset - 1] = '/n';//将最后一个逗号换成换行符。
printf(s);
return 0;
}
time_t t = time(0);
//产生"YYYY-MM-DD hh:mm:ss"格式的字符串。
char s[32];
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", localtime(&t));
①获取System时间: void GetSystemTime(LPSYSTEMTIME lpSystemTime); 下面是例子:
#include <stdio.h>
#include <stdlib.h>
void main() {
SYSTEMTIME st; //定义存放时间的结构体
char strTime[256];
int n=0;
GetSystemTime(&st);
n = sprintf(strTime,"Year:/t%d/n",st.wYear);
n += sprintf(strTime+n,"Month:/t%d/n",st.wMonth);
n += sprintf(strTime+n,"Day:/t%d/n",st.wDay);
n += sprintf(strTime+n,"Date:/t%d/n",st.wDayOfWeek);
n += sprintf(strTime+n,"Hour:/t%d/n",st.wHour);
n += sprintf(strTime+n,"Minute:/t%d/n",st.wMinute);
n += sprintf(strTime+n,"Second:/t%d/n",st.wSecond);
n += sprintf(strTime+n,"MilliSecond:/t%d/n",st.wMilliseconds);
printf("%s",strTime);
system("pause");
}
------------------------------------------------------------------
http://topic.csdn.net/t/20050831/10/4241344.html
用了5、6年C,竟然不知道printf/sprintf有这样的问题
#include <stdio.h>
void main()
{
long lTest = 11;
printf("%d,%d,%d,%d",lTest,lTest,22,33);
}
用BC30编译后DOS下运行结果是。。。
11 0 11 0
用VC60编译后window下运行结果。。。
11 11 22 33
用gc编译后unix下运行结果。。。
11 11 22 33
开发项目是一个DOS程序,所以结果老是不对,调了半天才知道问题是出在类型转换上
估计是因为long型在16位的系统上长度是4字节,而整型是2字节; 在32的系统上,long和int都是4字节所致
结论:用print和sprint都要小心一些,如果变量类型不对,函数进行自动转换有可能会在不同的平台上产生不同结果。谨记之。
- sprintf 用法 注意事项
- sprintf 用法 注意事项
- sprintf用法
- sprintf用法
- sprintf用法
- sprintf用法
- sprintf用法
- sprintf用法
- sprintf用法
- sprintf 用法
- sprintf用法
- sprintf()用法
- sprintf用法
- .sprintf 用法
- sprintf用法
- sprintf用法
- sprintf 用法
- sprintf用法
- 删除注释
- hibernate通用方法
- TCPL中的建议
- 几个预编译指令的用法
- Hibernate学习笔记:混合使用每个继承结构一张表 和 每个子类一张表
- sprintf 用法 注意事项
- 读取xml通过deserialize和linq to sql快速更新数据表
- java运行时多态性的实现
- Oracle load sqlldr details
- CSS 黑色 横向三级菜单
- java定时器的使用(Scheduler )
- OMS,Ophone,Iphone,android之间关系
- linux下静态库、动态库的编译和使用
- Css 圆角菜单