😸

[C] sprintf 関数

に公開

機能

文字列のフォーマット出力[1]

使用例

123, abc と表示する

\n は改行を表す エスケープシーケンス[2]

コード

char buf[32];
sprintf(buf, "%d, %s", 123, "abc");
printf("%s\n", buf);
全文
#include <stdio.h>
void main() {
    char buf[32];
    sprintf(buf, "%d, %s", 123, "abc");
    printf("%s\n", buf);
}

実行結果

123, abc

score: 85 を変数から表示する

コード

int score = 85;
char buf[16];
sprintf(buf, "score: %d", score);
printf("%s\n", buf);
全文
#include <stdio.h>
void main() {
    int score = 85;
    char buf[16];
    sprintf(buf, "score: %d", score);
    printf("%s\n", buf);
}

実行結果

score: 85
脚注
  1. 変数を指定した書式で文字列に埋め込む関数。printfの出力先を文字列にしたもの。 ↩︎

  2. 改行やタブなど、画面に表示されない制御文字のこと。 ↩︎

Discussion