0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sprintf()の機能

Last updated at Posted at 2019-10-19

フォーマット指定の文字列を作成できる

 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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?