Learn more about the usage of printf and the differences between various types of Deformation

Source: Internet
Author: User

Int printf (const char * format,...); Outputs Information to the default standard output device according to the specified format. The returned value is the number of printed characters, excluding the Terminator. For example, if printf ("1234") is executed normally, the return value of printf ("% d", 1234) is 4, and the output fails (not all characters are output), a negative number is returned.

Int fprintf (File * stream, const char * format,...); Outputs Information to the specified output device in the specified format, the return value is the same as above
Int sprintf (char * STR, const char * format,...); Outputs Information to the specified memory buffer in the specified format. The return value is the same as the preceding one.
Int snprintf (char * STR, size_t size, const char * format ,...); outputs a string to the buffer with the specified length in the specified format. If the output string exceeds the buffer length, the length of the output buffer is reduced by 1 character, and the string ending character \ 0 is added at the end, if the buffer length is greater than the string length, all strings are output to the buffer, and \ 0 is added at the end. the return value is the same as the preceding one.


Int vprintf (const char * format, va_list AP );
Int vfprintf (File * stream, const char * format, va_list AP );
Int vsprintf (char * STR, const char * format, va_list AP );
Int vsnprintf (char * STR, size_t size, const char * format, va_list AP );

The above four printf with V are actually designed for users to re-implement variable parameter functions. For example, if we need to implement a log function, its function is to print logs, the printed information is output to the serial port, and the input parameters are variable. How to implement it?

Int log (int A, int B, int C, const char * format ,...) {va_list AP; char STR [100]; // process related information va_start (AP, format); (void) vsnprintf (STR, sizeof (STR), format, AP ); va_end (AP); // Serial Output STR}
The above is a simple example, Printf with V headers is used for passing variable parameters when function Nesting is called. Printf without v cannot be called by passing variable parameters.

Printf is optimized in the integrated development environment Keil, and the following variants are available:

Int _ printf (const char * format ,...);
Int _ fprintf (File * stream, const char * format ,...);
Int _ sprintf (char * STR, const char * format ,...);
Int _ snprintf (char * STR, size_t size, const char * format ,...);
Int _ vprintf (const char * format, va_list AP );
Int _ vfprintf (File * stream, const char * format, va_list AP );
Int _ vsprintf (char * STR, const char * format, va_list AP );
Int _ vsnprintf (char * STR, size_t size, const char * format, va_list AP );

The above-mentioned underlined prinf version uses Keil to optimize the prinf implementation for embedded devices, sacrificing support for floating-point formats, but greatly reducing the code space.

Learn more about the usage of printf and the differences between various types of Deformation

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.