Use of the printf () function

Source: Internet
Author: User
Tags clear screen

From: http://www.cnblogs.com/adslg/archive/2008/08/22/1274164.html

Printf () is a formatting output function, which is generally used to output information to the standard output device in the specified format.

The call format of the printf () function is: printf ("<Format String>", <parameter table> );

The formatted string contains two parts:

Some are normal characters, which will be output as is;

The other part is the formatting rule 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 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.
1. format the operator
--------------------------
% 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.
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

--------------------------

\ 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.
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 is over 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);/* outputs the floating point number four digits after the decimal point.
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 decimal places in 18 digits
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.9s \ 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.

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.