C format output

Source: Internet
Author: User

I. printf () function

Printf () is a formatting output function, which is generally used to output information to the standard output device in the specified format. This function is often used in programming. The call format of the printf () function is: printf (\ "<formatted string >\", <parameter table> );

The formatted string consists of two parts: one is a normal character, which is output as is, and the other is a required character, which starts with \ "%, it is followed by one or more specified characters to determine the output content format.
A parameter table is a series of parameters that need to be output. The number of parameters must be the same as the number of output parameters described in the formatted string. The parameters are separated by \ ", \" and are sequentially matched, otherwise, unexpected errors may occur.

Ii. format the operator
The formatting rules provided by Turbo c2.0 are as follows:
When there are too many threads, there are too many threads, too many threads.
Symbol function
--------------------------
% D decimal signed integer
% U decimal unsigned integer
% F floating point number
% S string
% C single character
% P pointer Value
% E exponential floating point number
% X, % x unsigned integer in hexadecimal format
% 0 unsigned integer in octal format
% G automatically selects the appropriate notation
When there are too many threads, there are too many threads, too many threads.

You can insert a number between \ "% \" and a letter to indicate the maximum field width.

3. Description

NOTE 1: For d %

(1) % MD: Specify the output width. The number of data digits is smaller than m, left side fill space; greater than m, output by actual number of digits. For example, % 3d indicates that three integer values are output, and the three digits are not right aligned.

A = 123; B = 12345;
Printf (\ "% 4D, % 4D \", a, B );
Output result: _ 123,12345

If you want to add some 0 before the output value, add 0 before the field width. For example, % 04d indicates that when a value smaller than four digits is output, the total width of the value is set to 4 by adding 0 in front.

(2) % ld: Output long integer data. For example, % d cannot be used below.

Long A = 123456;
Printf (\ "% LD \", );
Printf (\ "% 9ld \", a); output: ___ 123456

(3) %-MD:

You can control the left alignment of the output, that is, you can add a \ "-\" number between the \ "% \" and the letter. If there is no description, the right alignment is displayed.
For example, %-7d indicates that the output 7-digit integer is left aligned.

NOTE 2: For F %

(1) % m. NF

Represents the largest field width m, decimal place is N bits, the integer is the m-n-1 bits, enough M bits right alignment.

For example, % 9.2f indicates the floating point number with the output field width of 9. the decimal point is 2, the integer is 6, and the decimal point occupies one place. If the number is not 9, the right alignment is reached.

(2) %-M. NF can control the left alignment of the output.

(3) % lf indicates that the double floating point number is output.

Note 3: For % s, % E is the same

%-10 s indicates that the output is left aligned with 10 characters. If this parameter is not specified, the output is right aligned.
% 8 s indicates the output string of 8 characters, which is not 8 characters in the right alignment.
% 6.9 s indicates a string with a length of not less than 6 and not greater than 9. If the value is greater than 9, content after 9th characters will be deleted.

Note 4: Other notes

(1) exceeds the specified field width

If the length of a string or the number of integer digits exceeds the field width, It is output based on the actual length. For floating-point numbers, if the integer part of the number of digits exceeds the width of the specified integer, It is output according to the actual integer. If the decimal point width exceeds the specified decimal point width, the output is rounded to the specified width.

(2) floating point number indicates the output format of characters or integer values, such as % 6.9 s and % 6.9d
If a floating point is used to represent the output format of a character or integer, the number after the decimal point represents the maximum width, and the number before the decimal point represents the minimum width. If it is greater than the maximum width, content after the maximum width will be deleted.

For example, % 6.9 s indicates a string with a length of not less than 6 and not greater than 9. If the value is greater than 9, content after 9th characters will be deleted.

4. Special characters

When there are too many threads, there are too many threads, too many threads.
Character Function
--------------------------
\ N line feed
\ F clear screen and change pages
\ R press ENTER
\ T Tab character
\ Xhh indicates an ASCII code in hexadecimal notation,
Hh indicates one to two hexadecimal numbers.
When there are too many threads, there are too many threads, too many threads.

Bytes -----------------------------------------------------------------------------------------------------------------

Int printf (const char * format, [argument]);

Format:

% [Flags] [width] [. perc] [F | N | h | l] type

Specify the data output method as follows:

1. The meaning of type is as follows:

D signed 10-digit integer

I signed 10-digit integer

O signed octal integer

U unsigned 10-digit integer

X unsigned hexadecimal number, expressed in lowercase abcdef

X unsigned hexadecimal number, expressed in ABCDEF

F/f floating point number

E/e floating point in scientific format

G uses % f and % e to represent the smallest number of digits in the same G format as the floating point g, but expressed as an index.

C Single Character

S string

% Display percentage itself

P shows a pointer, and the near pointer is expressed as: XXXX

Far pointer: XXXX: YYYY

N connection parameters should be a pointer, where the number of written characters is stored

2. flags specifies the output format. The values and meanings are as follows:

No right alignment. Fill in 0 and space on the left.

-Align left and fill in spaces on the right

+ Add the symbol + or-before the number-

A space is used to display symbols only for negative numbers.

# No effect on type = c, s, d, I, u

When type = o, x, X, add \ '0 \ ', \ "0x \", \ "0X \" before the value respectively \"

Type = e, E, f, always use the decimal point

Type = g, G, in addition to the value 0, always show the decimal point 3. width is used to control the width of the displayed value. The values and meanings are as follows: n (n = 1, 2, 3 ...) the width must be at least n bits, and should not be filled with spaces.

0n (n =, 3...) must be at least n characters in width. If not, fill the space with 0 characters on the left. *

In the formula list, the next parameter is still width 4. prec, which is used to control the digits after the decimal point. The values and meanings are as follows:

No display by default

0 when type = d, I, o, u, x, no effect

Type = e, E, f, do not display decimal point

N (n = 1, 2, 3...) the maximum number of decimal places when type = e, E, f

Type = others, indicating the maximum width of the display .*

In the format list, the next parameter is still width.

5. F | N | h | l indicates whether the pointer is a remote pointer or an integer is a long integer.

F remote pointer

N near pointer

H short integer or single-precision floating point number

L long integer or double-precision floating point number

1. General Format

Printf (format control, output table column)

For example: printf (\ "I = % d, ch = % c/n \", I, ch );

Note:

(1) "format control" is a string enclosed by a dual-marker, also known as "Conversion Control string". It contains two types of information:

① Format description: it consists of "%" and format characters. It is used to convert the output data to the specified format output.

② Common character, that is, the character that needs to be output as is.

(2) "output table column" is the data to be output, which can be an expression.

(3) The general form of the printf function can be expressed

Printf (parameter 1, parameter 2 ,......, Parameter n)

The function is to set parameters 2 ~ Parameter n is output in the format specified by parameter 1.

2. format characters (9 types)

(1) d (or I) format. It is used to output a decimal integer. It can be used in the following ways:

① % D, output according to the actual length of integer data.

② % Md, m is the width of the specified output field. If the number of digits of the data is smaller than m, spaces are filled on the left side. If the number is greater than m, the data is output based on the actual number of digits.

③ % Ld (% mld), which outputs long integer data.

Example: long a = 123456;

Printf (\ "% ld \", );

(2) o Format operator, which outputs integers in the form of Octal numbers. Format: % o, % mo, % lo, % mlo.

(3) x (or X) format operator, which outputs integers in hexadecimal format. Format: % x, % mx, % lx, % mlx.

(4) u format, used to output unsigned data, that is, the number of unsigned data, in decimal format. Format: % u, % mu, % lu.

See: li4-3.c/* output of unsigned data */

(5) The c format character, used to output a character. Format: % c, % mc.

(6) s format character, used to output a string. Format: % s, % ms, %-ms, % m. ns, %-m. ns.

See: li4-5.c/* string output */

(7) f format character, used to output real numbers (including Single and Double Precision), in decimal form. Format: % f, % m. nf, %-m. nf.

Note: The valid digits of Single-precision real numbers are generally 7 bits and the double precision is 16 bits.

See: li4-6.c/* Number of valid digits in the output of Single-Precision Real Number */

Li4-7.c/* Number of valid digits when double precision real numbers are output */

Li4-8.c/* specify decimal places when outputting real numbers */

(8) e (or E) format operator, which outputs real numbers in exponential form. Format: % e, % m. ne, %-m. ne.

(9) g (or G) format character, used to output real numbers, according to the value size, automatically select the f format or eformat (select a type with smaller output width ).

3. Description

(1) Except for X, E, and G (in uppercase letters), other characters must be in lowercase;

(2) The "format control" string can contain escape characters;

(3) If you want to output the character "%", you should use two consecutive % in the "format control" string, for example:

Printf (\ "% f % \", 1.0/3 );

(4) For details about the format, see the following table.

Table 4.1 printf format characters

Description of format characters

D, I outputs integers in a signed decimal form (positive numbers do not output symbols)

O outputs integers in octal unsigned form (0 is not output)

X and X output integers in the hexadecimal notation (the leading character 0x is not output), and x is used to output a ~ of the hexadecimal number ~ F is output in lowercase, and X is output in uppercase letters.

U outputs integers in unsigned decimal form

C is output as a character and only one character is output.

S output string

F outputs Single and Double Precision in decimal form, and outputs 6 decimal places implicitly.

E, E outputs real numbers in exponential form

G and G use a format with a shorter output width in the % f or % eformat without outputting meaningless 0

Table 4.2 Additional printf format description characters

Character

Description

Letter l

Used as a long integer. It can be added before the format characters d, o, x, and u.

M (representing a positive integer)

Minimum data width

N (representing a positive integer)

Returns n decimal places to the real number. Returns n decimal places to the string, indicating the number of truncated characters.

-

The number or character output is left-aligned in the domain.

 

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.