C の sprintf()

構文:

int sprintf(char *str, const char *string,...); 

戻る:

If successful, it returns the total number of characters written excluding null-character appended in the string, in case of failure a negative number is returned . 

sprintf は String print の略です。コンソールに出力する代わりに、sprintf で指定された文字バッファーに出力を保存します。

C




// Example program to demonstrate sprintf()> #include> int> main()> {> > char> buffer[50];> > int> a = 10, b = 20, c;> > c = a + b;> > sprintf> (buffer,> 'Sum of %d and %d is %d'> , a, b, c);> > // The string 'sum of 10 and 20 is 30' is stored> > // into buffer instead of printing on stdout> > printf> (> '%s'> , buffer);> > return> 0;> }>

出力

Sum of 10 and 20 is 30 

時間計算量: O(n) ここで、n はバッファに格納されている要素の数です。
補助スペース: O(n) ここで、n はバッファに格納されている要素の数です。


あなたにおすすめ