Printf formatting output details

Source: Internet
Author: User

The general format of printf () format conversion is as follows:
% (Flags) (width) (. Prec) Type
Parameters enclosed in parentheses are optional parameters, while % and type are necessary. Next we will first introduce several types
The integer % d is converted into a signed decimal number.
The % u integer is converted to an unsigned decimal number.
The % O integer is converted into an unsigned octal number.
The % x integer is converted to an unsigned hexadecimal number and expressed in lowercase abcdef.
The % x integer is converted into an unsigned hexadecimal number, and abcdef is used to represent the floating point number.

The parameter of the % F double type is converted to a decimal number and rounded to six digits below the decimal point.
Parameters of the % E double type are printed in an exponential form. There is a number that is before the decimal point, the six digits are after the decimal point, and the exponent part is expressed in lowercase E.
% E has the same effect as % E. The only difference is that the exponent part is expressed by an uppercase E.
Parameters of the % G Double type are automatically printed in the format of % f or % E. The standard is determined based on the value to be printed and the specified number of valid digits.
The difference between % G and % G is the same. The only difference is that % E format is used for exponential printing.

Character and string
% C Integer Parameters are converted to the unsigned char type for printing.
The parameter % s pointing to the string will be output verbatim until null characters appear.
% P if the parameter is a void * pointer, it is displayed in hexadecimal format.

Prec has several situations
1. The minimum number of digits of a positive integer.
2. represents the number of decimal places in the floating point type
3.% G represents the maximum number of valid digits.
4.% s represents the maximum length of the string.
5. If it is a * symbol, the value of the next parameter is the maximum length.

Width is the minimum length of the parameter. If this column is not a value but a * symbol, it indicates that the next parameter is used as the parameter length.

Flags has the following situations:

+ When a negative number is printed, printf () prints a negative number. This flag adds a positive sign (+) before the positive number is printed ).
# This flag has different meanings based on the converted characters. Before the type is O (for example, % # O), a 0 value is printed before the octal value (indicating the octal value ).
Before the type is X (% # X), '0x 'is printed multiple times before the hexadecimal number is printed ', before the type is E, E, F, G, or G, a numeric value is forced to print the decimal point. When the type is g or G, the zero at the end of the decimal point and decimal point is also reserved.

0 when a specified parameter exists, the parameter without a number is set to 0. This flag is disabled by default, so white spaces are usually printed.

I recently reviewed the C language and found many previous knowledge points. I found them and reproduced an article with a very careful explanation.

Printf format output

Keywords:
Printf

Format code

A

ABC

Abcdefgh

% S

A

ABC

Abcdefgh

% 5s

####

# ABC

Abcdefgh

%. 5 S

A

ABC

ABCDE

% 5.5 s

####

# ABC

ABCDE

%-5 S

A ####

ABC ##

Abcdefgh

Printf format string

Format code

1

-12

12345

123456789

% D

1

-12

12345

123456789

% 6d

##### 1

###-12

#12345

123456789

%. 4D

0001

-0012

12345

123456789

% 6.4d

#0001

#-0012

#12345

123456789

%-4d

1 ####

-12 #

12345

123456789

% 04d

0001

-012

12345

123456789

% + D

+ 1

-12

+ 12345

+ 123456789

Printf format integer values

Format code

1

. 01

. 00012345

12345.6789

% F

1.000000

0.010000

0.000123

12345.678900

% 10.2d

##### 1.00

#### 0.01

##### 0.00

#12345.67

% E

1.000000e + 00

1.000000e-02

1.234500e-04

1.234568e + 04

%. 4e

1.20.e + 00

1.20.e-02

1.2345e-04

1.2127e + 04

% G

1

0.01

0.00012345

12345.7

Format code

6.023e23

 

% F

60229999999999975882752.000000

 

% 10.2e

60229999999999975882752.00

 

% E

6.023000e + 23

 

%. 4e

6.0230e + 23

 

% G

6.023e + 23

 

The format parameter is a string containing three object types:

* Copy the unformatted characters to the output stream.

* Conversion specification. Each specification causes zero or more items to be retrieved in the value parameter list.

* The following escape sequence. When copying data to the output stream, these sequences cause their related operations to be displayed on devices with this function:

\ Backslash

\ A warning

\ B Return

\ F form feed

\ N line feed

\ R press ENTER

\ T skip

\ V vertical hop

\ DDD is a one, two, or three-digit octal digit. These escape sequences are displayed as bytes with numeric values specified by the octal number.

  

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.