フォーマット指定の文字列を作成できる
printf()はフォーマット指定したものをコマンドプロンプトなどに出力することができた。一方で、sprintf()はフォーマット指定したものを文字列に入れることができる。
ざっくりいうと、sprintf()はprintf()の文字列(String)格納版である。
実行例
ソース
sprintfExample.c
# include<stdio.h>
int main(){
int a = 100;
char mojiretsu[20];
sprintf(mojiretsu, "aは%dです\n", a); //文字列に入れるだけ
printf(mojiretsu); //コマンドプロンプトに表示する
}
実行結果
sprintfExample.exe
$ gcc sprintfExample.c -o sprintfExample
$ sprintfExample
aは100です
まとめ
sprintf()はフォーマット指定で文字列を生成することができる(コマンドプロンプトには表示しない)。
詳しいことは以下のサイトを参考にしてほしい。
http://www.c-tipsref.com/reference/stdio/sprintf.html