How to format C ++ sprintf

Source: Internet
Author: User

Today, we will introduce in detail how to format C ++ sprintf. I hope that beginners can fully master this knowledge based on the Content introduced in this article and experience the different application methods that this language brings to us.

Name:

 
 
  1. Printf, sprintf-converts the output result to a specified format.
  2. Void printf (string format ,...);
  3. String sprintf (string format ,...);

Syntax:

  • Basic concepts of C ++ function templates
  • C ++ constant reference correct application method
  • Interpretation of c ++ stack template application code
  • Common application skills of C ++ clipboard
  • C ++ Doxygen implementation function sharing

No return value printf (string format ,...);

String sprintf (string format ,...);

Usage: The format usage of LPC (s) printf () is as follows. Some extended usage of Lynscar (Sean A Reith) is added.

The current version supports the following modifier formats ):

"" Put a space before a positive integer.

"+" Put a + sign before a positive integer.

"-" Align the left of the field. note: the standard (s) printf () presets are aligned to the right, but this is very strange for languages that are mainly string-based. this feature is reserved in order to maintain compatibility with C.

"|" Align the center of the column.

"=" If the string length is longer than the column size, use column mode ). the C ++ sprintf format is only valid for strings, and Other types are invalid. in column mode, the characters in the string will automatically wrap (wrap ).

"#" Table mode. Within the column, each word in the string will change to a line (that is, \ n is added after each word). It is only valid for the string.

N specifies the size of the column as an integer. use * to replace the integer to specify the column size with parameters. if n is 0, this column is filled with null values (zeros) except data. if n is not 0, in addition to data, this column will be placed into space or other specified characters.

". "N indicates that the output precision is n. A normal string is truncated after this (if precision n is greater than the column length, the column length is changed to precision n ). the table uses precision to specify the total number of columns (if no precision is specified, the table automatically calculates the most suitable output result ). precision is invalid for other data types.

":" N specifies the fs and accuracy. If n is 0, the space originally used to fill the column is replaced by a null value (zeros ).

The "@" parameter is an array. In addition to @, the corresponding format data is also used for each element in the array.

The characters in "'X'" single quotation marks (') are used to fill up spaces other than data in the entire column. (the default value is a blank character) (if a null value is specified before the column size and the 'X' string is specified to fill the column, the future prevails.) Note: if you want to fill a string with single or double quotation marks ('), you must use \' or \ "to avoid compiler checks.

The format of the specified type in the C ++ sprintf format is as follows:

  1. % No parameters are specified here, but all modified formats (modifier) are ignored in % output ).
  2. The O parameter is of the LPC data type.
  3. The s parameter is a string.
  4. The d and I parameters are integers and printed in decimal places.
  5. The c parameter is an integer and printed as a character.
  6. The o parameter is an integer and is printed in octal.
  7. The x parameter is an integer and is printed in hexadecimal notation.
  8. The X parameter is an integer and is printed in hexadecimal notation (A to F ).
  9. F floating point number.

Return: sprintf () returns a string in the specified format.

The above is the acceptance of C ++ sprintf formatting.

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.