[C ++] sprintf formatting Method

Source: Internet
Author: User
Reference: http://micro.mud.com.cn/gm/efuns/strings/sprintf.htm

Name: Printf, sprintf-converts the output result to a specified format.
Syntax: Void printf (string format ,...);
String sprintf (string format ,...);

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

Usage:

The following is the format usage of LPC (s) printf (). Lynscar (Sean A Reith) has added some extended usage.

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 ). this parameter is valid only 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 is as follows:

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

Return Value: Sprintf () returns a string in the specified format.
Author: Sean A. Reith (Lynscar)
Refer: Sscanf
Translation: Spock @ FF 97. Jul.23.
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.