跳轉到內容

C 語言入門/C 語言中的 sprintf 函式

來自華夏公益教科書,自由的教學資源

"sprintf" 函式使用格式化資料建立字串。從技術上講,這屬於標準 I/O 庫的一部分,需要以下宣告

   #include <stdio.h>

然而,它實際上是一個字串函式,需要與其他字串函式一起討論。"sprintf()" 的語法與 "printf()" 完全相同,只是在開頭多了一個引數,指向一個字串。它不像 "printf()" 輸出到標準輸出,而是輸出到字串中。例如

   /* csprntf.c */

   #include <stdio.h>

   int main()
   {
      char b[100];
      int i = 42;
      float f = 1.1234f;
      sprintf( b, "Formatted data:  %d / %f", i, f );
      puts( b );
   }

—列印字串

   Formatted data:  42 / 1.1234

還有一個 "sscanf()" 函式,類似地反映了 "scanf()" 的功能。

C 字串函式庫

[編輯 | 編輯原始碼]

已移動到 C 語言入門/C 字串函式庫

華夏公益教科書