Sprintf function Usage Details, sprintf function details

Source: Internet
Author: User

Sprintf function Usage Details, sprintf function details
Function

Write formatted data to a character buffer.

Header files

Stdio. h

Prototype

Int sprintf (char * buffer, const char * format, [argument]… );

Parameter List

Buffer: a char pointer pointing to the buffer of the string to be written.

Format: format the string.

[Argument]...: optional parameter, which can be any type of data.

Return Value

Returns the number of characters written to the buffer. If an error occurs,-1 is returned. if the buffer or format is a null pointer and continues without errors, the function returns-1 and errno is set to EINVAL.

Sprintf returns the number of bytes written to the buffer when argument is in format, the ending character '\ 0' is not included (this function will automatically add' \ 0' to the end '). That is, if "Hello" is written into a buffer with enough space, the sprintf function returns 5. At the same time, the buffer content will be changed.

Example

J = sprintf (buffer, "String: % s \ n", s );

J + = sprintf (buffer + j, "Character: % c \ n", c );

J + = sprintf (buffer + j, "Integer: % d \ n", I );

J + = sprintf (buffer + j, "Real: % f \ n", fp );

J + = sprintf (buffer + j, "Integer: % d % 02d", I, j); // % 02d indicates that if there are not two numbers, 0 is automatically filled, if the number is greater than two, it will not be affected.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.