生活随笔
收集整理的這篇文章主要介紹了
c语言 库打印函数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
函數(shù)
#include<stdio.h>int printf(const char *format, ... );int printf(const char *restrict format, ... );int fprintf(FILE *stream, const char *format, ... );int fprintf(FILE *restrict stream, const char *restrict format, ... );int sprintf(char *dest, const char *format, ... );int sprintf(char *restrict dest, const char *restrict format, ... );int snprintf(char *restrict dest, size_t bufsz, const char *restrict format, ... );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);
參數(shù)
stream -- 要寫入的輸出文件流bufsz -- 最多寫入bufsz-1個字符,再加上空終止符format?--?指向指定數(shù)據(jù)轉(zhuǎn)譯方式的空終止多字節(jié)字符串的指針Example
int 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;}/* 運行結(jié)果 */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實現(xiàn)strlen功能 */#include <stdio.h>#include <string.h>int main(){ char *str = "helloworld"; int slen = snprintf(NULL, 0, "%s", str); printf("%d - %ld\r\n",slen, strlen(str)); return 0;}#include <stdio.h>#include <stdarg.h>#include <time.h> 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);}
文章轉(zhuǎn)載自我?guī)熜值牟┛?#xff0c;內(nèi)容是我?guī)熜挚偨Y(jié)的,如果有轉(zhuǎn)載的,請注明出處,原文請點擊左下角原文鏈接
當你看到這里的時候,說明你已經(jīng)閱讀完上面的內(nèi)容
不管怎樣,感謝您有心或者無意的關(guān)注和支持
想獲取學習1024G資料,請點擊狀態(tài)欄公眾號福利按鈕
總結(jié)
以上是生活随笔為你收集整理的c语言 库打印函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。