logo

C の snprintf() 関数

このセクションでは、C プログラミング言語の snprintf() 関数について説明します。 snprintf は、stdio.h ヘッダー ファイルの事前定義されたライブラリ関数であり、標準の printf() 関数の出力を他のバッファにリダイレクトします。

snprint() 関数は、指定された文字列をバッファ領域内の一連の文字または値にフォーマットするために使用されます。 snprintf() 関数には、バッファ領域に格納される NULL 文字を含む最大文字数を表す 'n' 引数が含まれています。

snprintf 関数は、バッファーに挿入または書き込まれた文字数も返します。ただし、これらの文字は、print ステートメント内の printf() 関数または stdout ヘッダー ファイル内の文字によって返されるか表示されます。

C の snprintf() 関数

注: snprintf() 関数は、バッファのサイズとしてもカウントされる結果の出力の末尾に null 文字を挿入します。さらに、バッファは文字型の要素のみを格納する配列であり、文字列型の要素は格納されません。

C の snprintf() 関数の構文

以下は、C プログラミング言語の snprintf() 関数の構文です。

 int snprintf (char *str, size_t size, const char *format, ?); 

パラメーター:

str : 文字型配列バッファです。

SQLのsubstring_index

サイズ : バッファに保存できる最大文字数を定義します。

フォーマット : C 言語では、文字列は、stdio.h ヘッダー ファイルで定義されている printf() 関数と同じタイプの仕様を含む形式を定義します。

…: これはオプションの (…) パラメーターまたは引数です。

戻り値:

snprintf() 関数は、NULL 終了文字を含まずに、十分な大きさのバッファーに書き込まれたまたは保管された文字または値の数を返します。また、書き込まれた文字がバッファサイズより大きい場合は、負の値を返します。バッファ サイズが小さすぎる場合、指定された文字列は切り詰められるか、バッファ サイズまで縮小されます。

例 1: C の snprintf() 関数をデモするプログラム

C言語のsnprintf()関数を使用して、バッファサイズを確認し、バッファに入力された文字数を返すプログラムを作成してみましょう。

 /* create an example to use the snprintf function in c. */ #include #include int main () { // declare and initialize the char variable char *r = 'Javatpoint.com'; char buf[100]; // define the size of character type buffer /* use the snprintf() function to return the no. of character founded in the buffer area */ int n = snprintf (buf, 34, '%s 
', r); // 34 represents the size of buffer to store max characters // display the string stored in the buffer and count each character of the buffer area. printf (' The given string is: %s 
 Count the stored character: %d 
', buf, n); return 0; } 

上記のプログラムを実行すると、コンソール画面に所定の出力が生成されます。

 The given string is: Javatpoint.com Count the stored character: 16 

2nd実行

 The given string is: Javatpoint.com Count the stored character: -1 

ここで、最大入力文字を 34 から 14 に減らします。今回は負の数値を返し、バッファ サイズが指定された文字列より小さいことを示します。

例 2: C で snprintf() 関数を使用するプログラム

C プログラミング言語の snprintf() 関数を使用して、バッファーに文字を挿入し、バッファーから戻る例を作成してみましょう。

データ構造 Java
 #include #include int main () { char buf[200]; // define the size of character type buffer int ret_val, buf_size = 55; char name[] = &apos;David&apos;; // define string int age = 19; // use the snprintf() function to return the no. of character found in buffer area ret_val = snprintf (buf, buf_size, &apos;Hello friend, My name is %s, and I am %d years old.&apos;, name, age); /* check ret_value should be greater than 0 and less than the size of the buffer (buf_size). */ if ( ret_val &gt; 0 &amp;&amp; ret_val <buf_size) { printf (' buffer is written successfully! 
 '); %s
', buf); no. of characters read: %d', ret_val); } else not completely filled or written. %s 
', the return value: 0; < pre> <p> <strong>When we execute the above program, it produces the given output on the console screen.</strong> </p> <pre> Buffer is written successfully! Hello friend, My name is David, and I am 19 years old. No. of characters read: 53 </pre> <p>In the above program, we declared the character type buffer buf[200], and the buf_size variable can insert the maximum characters is 55. If the given statement is in the defined range, the snprintf() function returns the total no. of characters read from the buffer. </p> <p> <strong>2<sup>nd</sup> execution</strong> </p> <pre> Buffer is not completely filled or written. Hello friend, My name is David and The return value: -1 </pre> <p>When we define the buf_size as 35, the given statement is automatically truncated by the snprintf() function that returns a negative number (-1) and displays &apos;Buffer is not completely filled or written&apos;.</p> <hr></buf_size)>

上記のプログラムでは、文字型バッファ buf[200] を宣言し、buf_size 変数で挿入できる最大文字数は 55 です。指定されたステートメントが定義された範囲内にある場合、snprintf() 関数は合計数を返します。バッファから読み取られた文字数。

2nd実行

 Buffer is not completely filled or written. Hello friend, My name is David and The return value: -1 

buf_size を 35 として定義すると、指定されたステートメントは、負の数 (-1) を返す snprintf() 関数によって自動的に切り捨てられ、「バッファーが完全に埋められていないか、書き込まれていません」と表示されます。