logo

funkcja snprintf() w C

W tej sekcji omówimy funkcję snprintf() w języku programowania C. snprintf to predefiniowana funkcja biblioteczna pliku nagłówkowego stdio.h, która przekierowuje dane wyjściowe standardowej funkcji printf() do innych buforów.

Funkcja snprint() służy do formatowania podanych ciągów znaków w postaci serii znaków lub wartości w obszarze bufora. Funkcja snprintf() zawiera argument „n” reprezentujący maksymalną liczbę znaków, łącznie ze znakiem null, przechowywanych w obszarze bufora.

Funkcja snprintf zwraca również liczbę znaków wstawianych lub zapisywanych w buforze. Jednakże znaki te są zwracane lub wyświetlane przez funkcję printf() w instrukcji print lub znaki w pliku nagłówkowym stdout.

funkcja snprintf() w C

Uwaga: Funkcja snprintf() wstawia znak null na końcu wynikowego wyniku, który jest również liczony jako rozmiar bufora. Co więcej, bufor jest tablicą, która przechowuje tylko elementy typu znakowego, a nie typu łańcuchowego.

Składnia funkcji snprintf() w C

Poniżej znajduje się składnia funkcji snprintf() w języku programowania c.

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

Parametry:

ul : Jest to bufor tablicowy typu znakowego.

rozmiar : określa maksymalną liczbę znaków, które można przechowywać w buforze.

format : W języku C ciąg znaków definiuje format zawierający ten sam typ specyfikacji, jaki definiuje funkcja printf() w pliku nagłówkowym stdio.h.

…: Jest to opcjonalny (…) parametr lub argument.

Zwracane wartości:

Funkcja snprintf() zwraca liczbę znaków lub wartości, które zostały zapisane lub zapisane w wystarczająco dużym buforze, bez uwzględniania znaku kończącego zerowego. A jeśli zapisane znaki są większe niż rozmiar bufora, zwraca wartość ujemną. A jeśli rozmiar bufora będzie zbyt mały, podany ciąg zostanie obcięty lub zmniejszony do rozmiaru bufora.

Przykład 1: Program demonstrujący funkcję snprintf() w C

wzorce projektowe w Javie

Stwórzmy program sprawdzający rozmiar bufora i zwracający liczbę znaków wprowadzonych do bufora za pomocą funkcji snprintf() w C.

 /* 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; } 

Kiedy wykonamy powyższy program, wyświetli on dane wyjściowe na ekranie konsoli.

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

2IIwykonanie

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

Teraz zmniejszamy maksymalną liczbę znaków wejściowych z 34 do 14 i tym razem zwraca ona liczbę ujemną, wskazując, że rozmiar bufora jest mniejszy niż podany ciąg.

Przykład 2: Program korzystający z funkcji snprintf() w C

Stwórzmy przykład wstawiania znaku do bufora i powrotu z niego za pomocą funkcji snprintf() w języku programowania C.

 #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)>

W powyższym programie zadeklarowaliśmy bufor typu znakowego buf[200], a do zmiennej buf_size można wstawić maksymalnie 55 znaków. Jeżeli dana instrukcja mieści się w zdefiniowanym zakresie, funkcja snprintf() zwraca całkowitą liczbę znaków. znaków odczytanych z bufora.

2IIwykonanie

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

Kiedy zdefiniujemy buf_size jako 35, dana instrukcja zostanie automatycznie obcięta przez funkcję snprintf(), która zwraca liczbę ujemną (-1) i wyświetla komunikat „Bufor nie jest całkowicie wypełniony lub zapisany”.