c语言 库打印函数
发布日期:2021-06-30 18:55:46
浏览次数:2
分类:技术文章
本文共 4319 字,大约阅读时间需要 14 分钟。
函数
#includeint printf(const char *format, ... );/* [until c99]写结果到stdout */int printf(const char *restrict format, ... );/* [since c99] */int fprintf(FILE *stream, const char *format, ... );/* [until c99]写结果到文件流stream */int fprintf(FILE *restrict stream, const char *restrict format, ... );int sprintf(char *dest, const char *format, ... );/* [until c99]写结果至dest */int sprintf(char *restrict dest, const char *restrict format, ... );int snprintf(char *restrict dest, size_t bufsz, const char *restrict format, ... );/* [since c99]写结果至dest,至多写bufsz-1个字符。产生的字符串会以空字符终止,除非bufsz为0。若bufsz为0,则不写入任何内容,且dest可以是空指针,然而依旧计算返回值(会写入的字符数,不包含空终止符)并返回。 */int vprintf(const char *format, va_list ap);int vprintf(const char *restrict format, va_list ap);int vfprintf(FILE *stream, const char *format, va_list ap);int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap);int vsprintf(char *dest, const char *format, va_list ap);int vsprintf(char *restrict dest, const char *restrict format, va_list ap);int vsnprintf(char *restrict dest, size_t bufsz, const char *restrict format, va_list ap);
参数
stream -- 要写入的输出文件流bufsz -- 最多写入bufsz-1个字符,再加上空终止符format -- 指向指定数据转译方式的空终止多字节字符串的指针
Example
#includeint main(){ /* String test */ char *str = "hello"; printf("str = .%s.\r\n", str); printf("str = .%-10s.\r\n", str); printf("str = .%10s.\r\n", str); printf("str = .%-*s.\r\n", 10, str); printf("str = .%*s.\r\n", 10, str); printf("str = .%*.*s.\r\n", 10, 5, str); printf("str = .%*.*s.\r\n", 10, 2, str); printf("str = .%10.5s.\r\n", str); printf("str = .%10.2s.\r\n", str); /* Integer test */ printf("dec = .%d.\r\n", 555); printf("dec = .%10d.\r\n", 555); printf("dec = .%10.5d.\r\n", 555); printf("dec = .%-10.5d.\r\n", 555); printf("dec = .%*.*d.\r\n", 10, 5, 555); printf("dec = .%*.*d.\r\n", 10, 2, 555); printf("hex = .%x.\r\n", 555); printf("hex = .%X.\r\n", 555); printf("hex = .%#x.\r\n", 555); printf("hex = .%10x.\r\n", 555); printf("hex = .%10.5x.\r\n", 555); printf("hex = .%-10.5x.\r\n", 555); printf("hex = .%*.*x.\r\n", 10, 5, 555); printf("hex = .%*.*x.\r\n", 10, 2, 555); printf("oct = .%o.\r\n", 555); printf("oct = .%#o.\r\n", 555); printf("oct = .%10o.\r\n", 555); printf("oct = .%10.5o.\r\n", 555); printf("oct = .%-10.5o.\r\n", 555); printf("oct = .%*.*o.\r\n", 10, 5, 555); printf("oct = .%*.*o.\r\n", 10, 2, 555); /* Float test */ printf("flt = .%f.\r\n", 3.1415); printf("flt = .%lf.\r\n", 3.1415); printf("flt = .%10f.\r\n", 3.1415); printf("flt = .%10.5f.\r\n", 3.1415); printf("flt = .%-10.5f.\r\n", 3.1415); printf("flt = .%*.*f.\r\n", 10, 5, 3.1415); printf("flt = .%*.*f.\r\n", 10, 2, 3.1415); return 0;}/* 运行结果 */str = .hello.str = .hello .str = . hello.str = .hello .str = . hello.str = . hello.str = . he.str = . hello.str = . he.dec = .555.dec = . 555.dec = . 00555.dec = .00555 .dec = . 00555.dec = . 555.hex = .22b.hex = .22B.hex = .0x22b.hex = . 22b.hex = . 0022b.hex = .0022b .hex = . 0022b.hex = . 22b.oct = .1053.oct = .01053.oct = . 1053.oct = . 01053.oct = .01053 .oct = . 01053.oct = . 1053.flt = .3.141500.flt = .3.141500.flt = . 3.141500.flt = . 3.14150.flt = .3.14150 .flt = . 3.14150.flt = . 3.14. /* snprintf实现strlen功能 */#include #include int main(){ char *str = "helloworld"; int slen = snprintf(NULL, 0, "%s", str); printf("%d - %ld\r\n",slen, strlen(str)); return 0;}#include #include #include void debug_log(const char *fmt, ...){ struct timespec ts; timespec_get(&ts, TIME_UTC); char time_buf[100]; size_t rc = strftime(time_buf, sizeof time_buf, "%D %T", gmtime(&ts.tv_sec)); snprintf(time_buf + rc, sizeof time_buf - rc, ".%06ld UTC", ts.tv_nsec / 1000); va_list args1; va_start(args1, fmt); va_list args2; va_copy(args2, args1); char buf[1+vsnprintf(NULL, 0, fmt, args1)]; va_end(args1); vsnprintf(buf, sizeof buf, fmt, args2); va_end(args2); printf("%s [debug]: %s\n", time_buf, buf);} int main(void){ debug_log("Logging, %d, %d, %d", 1, 2, 3);}
文章转载自我师兄的博客,内容是我师兄总结的,如果有转载的,请注明出处,原文请点击左下角原文链接
当你看到这里的时候,说明你已经阅读完上面的内容
不管怎样,感谢您有心或者无意的关注和支持
想获取学习1024G资料,请点击状态栏公众号福利按钮
转载地址:https://linus.blog.csdn.net/article/details/88047860 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月14日 01时04分58秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
适合入门UI设计新人阅读的书!
2019-04-30
软件测试需要掌握哪些技术?
2019-04-30
ui设计培训学习就业大纲分享
2019-04-30
IT程序员面试技巧有哪些?
2019-04-30
美工掌握这些能力,不逊色于UI设计师
2019-04-30
如何入门UI设计?学ui设计要看什么书籍
2019-04-30
UI学习之后就业前景如何?
2019-04-30
教你如何做出有创意的作品
2019-04-30
UI设计师需要掌握的平面设计基础!
2019-04-30
零基础学UI设计能够学会吗?
2019-04-30
入门学习ui设计就业方向有哪些?
2019-04-30
软件测试的学习门槛高吗?
2019-04-30
如何开始学习软件测试?学习软件测试要做哪些准备工作?
2019-04-30
软件测试有哪些常用的方法?
2019-04-30
【JY】SignalData软件开发应用分享
2019-04-30
视频转码服务
2019-04-30
内联函数变量问题
2019-04-30
2021年6月,开发者社区更新。
2019-04-30