Hàm length trong c++


The snprintf() function is defined in the header file và is used to store the specified string till a specified length in the specified format.

Bạn đang xem: Hàm length trong c++

Characteristics of snprintf() method:

The snprintf() function formats và stores a series of characters & values in the array buffer.The snprintf() function accepts an argument ‘n’, which indicates the maximum number of characters (including at the over of null character) to be written to lớn buffer.The snprintf() function is used lớn redirect the output of printf() function onto a buffer.The snprintf() also returns the number characters that were supposed lớn be written onto the buffer (excluding the null terminator), irrespective of the value of ‘n’ passed.So, only when the returned value is non-negative và less than ‘n’, the string has been completely written as expected.

Syntax: Thesyntax of snprintf() method is:

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


*str : is a buffer.size : is the maximum number of bytes (characters) that will be written to lớn the buffer.

Xem thêm: Các Yếu Tố Ảnh Hưởng Đến Hành Vi Người Tiêu Dùng, Các Mô Hình Và Yếu Tố Ảnh Hưởng

format : C string that contains a format string that follows the same specifications as format in printf… : the optional ( …) arguments are just the string formats lượt thích (“%d”, myint) as seen in printf.

Return value:

The number of characters that would have been written on the buffer, if ‘n’ had been sufficiently large. The terminating null character is not counted.If an encoding error occurs, a negative number is returned.

Xem thêm: Nón Bảo Hiểm Quảng Cáo Mang Lại Lợi Ích Lớn Cho Doanh Nghiệp

Below is an example to illustrate the working of snprintf() method:

Example 1:


Writing zerovn.net onto buffer with capacity 6String written on buffer = geeksValue returned by snprintf() method = 14

The number of bytes printed lớn "buffer" (excluding the null terminator) is 45Joined string:The quick brown fox jumped over the lazy dog.



We use cookies to lớn ensure you have the best browsing experience on our website. By using our site, youacknowledge that you have read and understood ourCookie Policy & Privacy PolicyGot It !