Printf () Usage

Source: Internet
Author: User

From: http://baike.baidu.com/view/1427555.htm

 

Although the C language has been used for many years, it is still necessary to read books frequently to remember some subtle things. After searching for a few minutes on Uncle Google, I couldn't find the desired knowledge. As a result, my colleague Xing _^ found it in Baidu encyclopedia. Well, it seems that the specific situation of an item should be analyzed, you cannot draw conclusions.

Printf () function

Printf () is a formatting output function, which is generally used to output data to a standard output device in the specified format.
Information. This function is often used in programming. The function is prototype:
Int printf (const char * format ,...);
The Return Value of the function is an integer. If the result is successful, the number of output characters is returned, and if an error occurs, a negative value is returned.
The call format of the printf () function is:
Printf ("<formatted string>", <parameter table> );
The formatted string consists of two parts: one part is a normal character, which will be based on the original
Sample output. The other part is to format the specified character, starting with "%", followed by one or more specified characters,
Used 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 that described in the formatted string.
There are as many parameters as they are. "," are used to separate them and the order is one-to-one. Otherwise, you may want
Error.
Note: The function printf reads data from left to right, then puts the read data first to the bottom of the stack, and finally puts the read data on the top of the stack. The processing starts from the top of the stack. All the results we can see are, from the right.
1. 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.


Role of a symbol

--------------------------
% 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
% O 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.
Note:
(1). You can insert a number between "%" and a letter to indicate the largest field width.
For example, % 3d indicates that three integer values are output, and the three digits are not right aligned.
% 9.2f indicates the floating point number with the output field width of 9, where the decimal point is 2 and the integer is 6,
The decimal point occupies one place, which is not 9 digits right-aligned.
% 8 s indicates the output string of 8 characters, which is not 8 characters in the right alignment.
If the length of a string, or the number of integer digits exceeds the field width, It is output according to the actual length.
However, for floating-point numbers, if the number of digits in the integer part exceeds the width of the entire digit, the data 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.
In addition, if you want to add some 0 before the output value, you should add 0 before the field width.
For example, % 04d indicates that when a value smaller than four digits is output, 0 is added before to make the total width
The value is 4 digits.

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,
The number before the decimal point represents the minimum width.
For example, % 6.9 s indicates that a string with a length of not less than 6 and not greater than 9 is displayed. If the value is greater than 9
Content after 9th characters will be deleted.
(2) You can add the lower-case letter l between "%" and the letter to indicate that the output is a long number.
For example, % LD indicates the output long integer.
% Lf indicates that the double floating point number is output.
(3) You can control the Left or Right alignment of the output, that is, you can add a "-" number between "%" and letters.
The output is left-aligned. Otherwise, the output is right-aligned.
For example, %-7d indicates that the output 7-digit integer is left aligned.
%-10 s indicates that the output is left aligned with 10 Characters
2. Some special characters
When there are too many threads, there are too many threads, too many threads.


Role of B

--------------------------
/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.
The printf () function learned in this section, combined with the data types learned in the previous section, describes the procedure
To enhance understanding of the Data Types of Turbo c2.0.


Example

# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char C, S [20], * P;
Int A = 1234, * I;
Float F = 3.141592653589;
Double X = 0.12345678987654321;
P = "How do you do ";
Strcpy (S, "Hello, comrade ");
* I = 12;
C = '/x41 ';
Printf ("A = % d/N", a);/* the output result is a decimal integer a = 1234 */
Printf ("A = % 6D/N", a);/* output 6-digit decimal number A = 1234 */
Printf ("A = % 06d/N", a);/* output 6-digit decimal number A = 001234 */
Printf ("A = % 2D/N", a);/* A exceeds 2 bits and Outputs A = 1234 */
Printf ("* I = % 4D/N", * I);/* output a four-digit decimal integer * I = 12 */
Printf ("* I = %-4D/N", * I);/* the output is left aligned with a four-digit decimal integer * I = 12 */
Printf ("I = % P/N", I);/* output address I = 06e4 */
Printf ("F = % F/N", f);/* output floating point number f = 3.141593 */
Printf ("F = 6.4f/N", f);/* output 6 floating point numbers with four decimal places
F = 3.1416 */
Printf ("x = % lf/N", x);/* output long floating point number x = 0.123457 */
Printf ("x = % 18.16lf/N", x);/* output a long floating point with 16 digits after the decimal point.
Number X = 0.1234567898765432 */
Printf ("c = % C/N", c);/* output character c = */
Printf ("c = % x/N", c);/* the ASCII code value of the output character c = 41 */
Printf ("s [] = % s/n", S);/* output array string s [] = Hello, Comrade */
Printf ("s [] = % 6.9 S/N", S);/* output string s [] = Hello,
Co */
Printf ("s = % P/N", S);/* output array string first character address S = ffbe */
Printf ("* P = % s/n", P);/* output pointer string P = How Do You do */
Printf ("P = % P/N", P);/* output pointer value p = 0194 */
Getch ();
Retunr 0;
}
The address values in the preceding results may be different on different computers.
In the example, the first statement # include <stdio. h> indicates that another file stdio. H is called.
Is a header file that includes data type definitions and function descriptions for all standard input/output library functions.
The variables and function types used by each library function have been defined and described in Turbo c2.0.
In the header file "*. H", you must use # include <*. h> or # include "*. H" when using these functions"
Statement to call the corresponding header file for connection. If this statement is not used, an error occurs during connection.
Error.
Application of printf () and sprintf () in PHP
<? PHP
Printf ("$ % 01.2f", 43.2); // running result: $43.20
Echo"
";
Printf ("% d bottles of beer on % s", 100, "the wall ");
Echo"
";
// Running result: 100 bottles of beer on the wall
Printf ("% 15 s", "some text"); // running result: some text
?>
<? PHP
Echo"
";
Printf ("the % 2/$ s likes to % 1/$ s", 111, dog );
Echo"
";
// Running result: The dog likes to bark
Printf ("the % 1/$ s says: % 2/$ S, % 2/$ S.", "dog", "Bark ");
// Running result: the dog says: Bark, bark.
?>
<? PHP
Echo"
";
$ Var1 = 68.75;
$ Var2 = 54.35;
$ Var3 = $ var1 + $ var2;
Echo $ var3;
Echo"
";
// Variable $ var3 value is "123.1 ";
$ Formatted = sprintf ("% 01.2f", $ var3 );
Echo"
";
Echo $ formatted;
// Variable $ var3 value is "123.10"
?>
<? PHP
Echo"
";
$ Money = 1.4;
$ Formatted = sprintf ("%-01.2f", $ money );
Echo $ formatted;
?>
Result:
$43.20
100 bottles of beer on the wall
Some text
The dog likes to 111
The dog says: Bark, bark.
123.1
123.10

1.40

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.