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 ("<Format String>", <parameter table> );

The formatted string consists of two parts: some are normal characters, which are output as is, and the other part is the required characters, which start 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 commas (,) and the order is one to one, 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 "-" sign between "%" and a 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 the screen and change the page
/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.

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.