Description of most printf Parameters

Source: Internet
Author: User

# Include

/*************************************** *********************************/
//
// Author: flyingleaf
//
// Function: simple test of the printf type, mainly to help mm view the abnormal printf Parameters
//
// Date: 2004-12-27
//
// Test tool: vc7.0
//
/*************************************** *********************************/

Int _ tmain (INT argc, _ tchar * argv [])
{
Float d = 3.14;
Printf ("d = %. 10E \ n", d); // the output of six digits is junk data, which is inaccurate.

Char ch [20];
Strcpy (CH, "123456780123 ");
Int n = 2, M = 10;
// *. * What about the front edge? * defines the total width and the number of outputs.
// If the back side is smaller than the front side, use spaces to fill up M places on the left side.
Printf ("% *. * s \ n", M, N, CH); // output "12" here"
Printf ("% *. * s \ n", n, m, CH); // output "1234567890" here"
// Supplement: If strlen (CH) <m, the strlen bit is output, for example:
Strcpy (CH, "1234678 ");
// A hh is used as the output at the end of this example to better display the cursor at the end of the output 12345678.
Printf ("% *. * Shh \ n", M, N, CH); // output "12hh" here"
Printf ("% *. * Shh \ n", n, m, CH); // output "12345678hh" here"

Int y= 456;
// Here # 8D, keep the width. If it is not 8 characters long enough, fill it with spaces on the left.
// If the number exceeds 8 bits, the number of BITs is output.
Printf ("% # 8d \ n % # 8x \ n % # 8o \ n", Y, Y, y );
Printf ("% # 3D \ n % # 3x \ n % # 3O \ n", Y, Y, y );
Printf ("% # 1D \ n % # 1x \ n % # 1O \ n", Y, Y, y );

// The. 8d here is estimated to be familiar to everyone, right? If the number is not 8, the left side must be filled with 0.
// Similarly, if there are more than 8 bits, several BITs are output.
Printf ("%. 8d \ n %. 8x \ n %. 8o \ n", Y, Y, y );

// Here, * D is estimated that some people are a little unfamiliar. In fact, it can be regarded as # 6d, and the effect is the same.
Printf ("% * d \ n", 6, y );

// Here, the "+" in "% + 6D" two meanings: 1. There is a "+" in front of the output number, and 2. There are not 6 spaces on the left.
Printf ("% + 6D \ n", y );
// The number of connections is basically the same as that of % + 6D above. However, if the number of digits of Y + 1 is not 6, 0 is used, but the number cannot exceed
// The number of zeros before 6. Run the following command to check the specific effect:
Printf ("% + 006d \ n", y );
Printf ("% + 0006d \ n", y );
// Add that if the number of digits 6 is smaller than the number of digits y, only the number + and Y are output.
// If there is no plus (+), use 0 to add 6 digits. For example:
Printf ("% 06d \ n", Y); // output "000456"

// Here, the-sign is a space fill on the right. For the sake of clarity, we still use HH as the end.
Printf ("%-6dhh \ n", y );
// Of Course, if 2 is not as large as Y, then output y directly and then output HH
// Here, the "-" is only the meaning of adding spaces on the right.
Printf ("%-2dhh \ n", y );

// An example output using printf
Int Len = 0;
// Here % N indicates giving the length of the string before % N to Len:
// The example below is 8 = strlen ("hh") + strlen ("123456 ");
Printf ("HH % S % N \ n", "123456", & Len );
Printf ("Len = % d \ n", Len );

// I didn't want to write about him, but I should take it with me.
// Simple Description:. 0f is the first digit after the decimal point, without a dot #.-0f is a dot, but it is also a zero digit.
// While % G is omitted. Therefore, invalid 0. If there is no decimal point, a 0 value cannot be less without a vertex # G!
Printf ("%. 0fhh \ n % #. 0fhh \ n % ghh \ n % # ghh \ n", 3.0, 3.0, 3.0 );

// An incomprehensible one. Here, "C traps and defects" indicates that 7 shells are output and then the "%" number is output. How can I try it with a % number.
// Who tried the results are not the same, please tell me: csflyingleaf@163.com Thank you
Printf ("% * % \ n", 8 );

// Well, I think it's a lot more. If it's not enough, send me a text message.
Return 0;
}

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////

Printf () function

Function: Outputs a number of data types to the terminal (putchar can only output characters and can only be one character, while printf can

Output multiple data types)

I. general formats of printf ()

Printf (format control, output list );

Example:

Int I = 3;
Double F = 4.56;
Printf ("I = % d, f = % F \ n", I, F );

Printf () is a function, and "format control" and "output list" are its parameters. It can be expressed as: printf (parameter 1, parameter 2, parameter 3 ,...., parameter n). "parameter 1" indicates "format control", and other parameters indicate "output list.

Ii. format characters

 
1. d format character: Output in decimal format.

% D The length of the output number is the actual length of the variable value.
% MD M specifies the width of the output data. When the actual width of the data is smaller than m, spaces are filled on the left side of the data. If the width is greater than m, the data is output based on the actual number of digits.
% LD, % MLD L (lowercase letter L) indicates the output of "long integer" Data
% 0md, % 0mld 0 (number 0) indicates 0 when the number of digits is less than m

Note: %. M (digit control) and 0 (digits less than 0) are also applicable to other format characters.

Example:(□Space)
Int I = 123;
Long J = 123456;
Printf ("% d □5d □05d, □ld □8ld □08ld", I, J );

123 □□□123 □00123, □123456 □□□123456 □00123456

 
2. O (letter) format character: returns an integer in octal format. (No negative number format)
 
3. x format character: returns an integer in hexadecimal format. (No negative number format is displayed)
 
4. U format character: returns the unsigned integer in decimal format.
 
[Example: 3.2]
Main ()
{ Unsigned int A = 65535;
  Int B =-2;
  Printf ("A = % d, % O, % x, % u \ n", );
  Printf ("A = % d, % O, % x, % u \ n", B );
}  

A =-65535 177777, FFFF

B =-65534, 177776, fffe

 
5. c format character: Used to output a character. An integer ranging from 0 ~ It can also be output in character format between 255

Example:
Char C;
Printf ("% C", C );

An integer ranging from 0 ~ Within the range of 255, it can also be output in the form of characters. Before the output, convert the integer to the corresponding ASCII character. Conversely, a single character data can also be output in integer form.

[Example: 3.3]
Main ()
{ Char c = 'a ';
  Int I = 97;
  Printf ("% C, % d \ n", C, C );
  Printf ("% C, % d \ n", I, I );
}  
The running result is:
A, 97
A, 97
 
6. s format character: Used to output a string.
% S Used to output a string without double quotation marks. For example: printf ("% s", "China ");
% Ms Width specified by M (when the string length is smaller than m, the left space is filled. If the string length is greater than m, the output is based on the actual width)
%-MS Left alignment
% M. NS The output occupies the m column and only takes n characters from the left of the string. The N characters are output to the right of the m column and left spaces are filled.
%-M. NS Same as above, right fill space
 
7. F format OPERATOR: Output in real number format.
% F All integers are displayed, and the decimal part is 6 digits. However, not all digits are valid digits.
% M. NF The width of the specified data is in the m column, where n decimal places exist. If the length of the value is smaller than m, spaces are filled on the left.
%-M. NF Similar to % M. F, only spaces should be filled on the right.
[Example: 3.5]
Main ()
{ Float X, Y;
  X = 111111.111; y = 222222.222;
  Printf ("% F", x + y );
}  
ProgramOutput:
  333333.328152 (errors in real number operations are inevitable)
 
[Example: 3.6]
Main ()
{ Double X, Y; double X2, Y2;
  X = 1111111111111.111111111; y = 2222222222222.222222222;
  X2 = 1111111111111.111; y2 = 2222222222222.222;
  Printf ("% F", X + Y, X2 + y2);/* 13-digit integer, 9-digit decimal */
}  
Program output:
  3333333333333.333010 3333333333333.333010 (Same)
 
We can see from [Example 3.6] and [Example 3.7:
 
(1) errors in real number operations are inevitable.
 
(2) double (example 3.7) is more accurate than float (example 3.6.
 
(3) The valid digits of Float real numbers (single precision) are 7 digits, and the double real numbers (Double Precision)
The number of valid digits is 16 bits. output and input that exceed the valid BITs have no significance.
[Example: 3.7]
Main ()
{  
  Float F = 123.456;
  Printf ("% F □□□% 10f □□% 10.2f □□%. 2f □□%-10.2f", F );
}  
Output result:
  123.455994 □□123.455994 □□□□□123.46 □□123.46 □□123.46
 
8. egex: returns the real number in exponential form.
% E Output real numbers in the form of normalized indexes. The system automatically gives 6 decimal places, and the index part occupies 5 digits.
% M. ne Same as described above
%-M. ne Same as described above
 
9.g format character: it will automatically select the F format or E format to output data based on the value size, and it will not output meaningless 0.
 
III,Usage notes
 
% Is the format specifier of printf (). To directly output the character %, use two consecutive % in format control.
 
Example:

Printf ("% F %", 1.0/3)

Output: 0.333333%.

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.