Printf data output statement and escape characters

Source: Internet
Author: User
Tags printable characters

Data Output statement

This section describes how to output data to a standard output device display. In C language, all data input/output is completed by library functions. Therefore, they are all function statements. This section describes the printf and putchar functions. The printf function is called the format output function. The last letter of the keyword "F" indicates "format. The function is to display the specified data to the display screen in the format specified by the user. We have used this function multiple times in the previous example.

I. general form of printf function call

The printf function is a standard library function. Its function prototype is in the header file "stdio. H. However, as a special case, you do not need to include stdio. H files before using the printf function. The printf function is generally called in the form of printf ("format control string", output table column). The format control string is used to specify the output format. The format control string can be a format string or a non-format string. The format string is a string starting with "%". It is followed by characters in different formats to indicate the type, format, length, and decimal places of the output data. For example, "% d" indicates output in decimal Integer type, "% lD" indicates output in decimal long integer type, and "% C" indicates output in numeric type. We will discuss it later.

The unformatted string is printed as it is when it is output, and serves as a prompt in display. Each output item is provided in the output table. The format string and each output item must correspond one to one in quantity and type.

Ii. Format String

In Turbo C, the format string is generally [flag] [minimum output width] [. Precision] [length] type. The items in square brackets [] are optional. The meanings of each item are described as follows:

1. Type type characters are used to indicate the type of output data. The format characters and meanings are shown in the following table:

Meaning of Characters in the output format
D. Output signed integers in decimal form (positive numbers do not output symbols)
O outputs unsigned integers in octal form (no prefix o is output)
X outputs an unsigned integer in hexadecimal format (the ox prefix is not output)
U outputs an unsigned integer in decimal format
F outputs Single and Double Precision Real Numbers in decimal form
E outputs Single and Double Precision Real Numbers in exponential form
G outputs Single and Double precision real numbers with a shorter output width in % F % E
C Outputs a single character
S output string
      
2. Flag

The following table lists the four types of Logo characters:-, +, #, and space:

Logo format character logo meaning
-The result is left aligned with a space on the right.
+ Output symbol (positive or negative)
If the output value of a space parameter is positive, it is preceded by a space.
# It has no effect on Class C, S, D, and U, and adds a prefix to class o in output. For Class X, the prefix 0x is added to the output. for Class E, G, and F, the decimal point is given when the result contains decimal places.
     
3. Minimum output width

The minimum number of digits of the output is expressed by a decimal integer. If the actual number of digits is greater than the defined width, the actual number of digits is output. If the actual number of digits is less than the defined width, spaces or 0 are supplemented.

4. Precision

The precision format character starts with "." and is followed by a decimal integer. The meaning of this item is: if a number is output, it indicates the number of digits in decimal places. If the output is a character, it indicates the number of output characters. If the actual number of digits is greater than the defined precision, the part that exceeds the limit is truncated.

5. Length

The length format is H and l. h indicates output by short integer, and l indicates output by long integer.

Example
"%-MD": Left alignment. If the M ratio is small, it is output as needed.
"% M. Ns": output M-bit. Take the string (from left) N-bit, left fill space, when n> m or M is omitted, M = N
E.g. "% 7.2 s" input China output "ch"
"% M. NF": Output floating point number. M indicates the width, and N indicates the right digit of the decimal point.
E.g. "% 3.1f" input 3852.99 output 3853.0

6. Special usage
The format of M. N can also be expressed as follows (for example)
Char ch [20];
Printf ("% *. * s/n", M, N, CH );
* Defines the total width and the number of outputs. The Parameter M and N correspond to the outer parameters respectively. I think the advantage of this method is that the M and N parameters can be assigned outside the statement to control the output format.
Today (06.6.9), we see another output format: % N. You can assign the length value of the output string to another variable, as shown in the following example:
Example:
Int slen;
Printf ("Hello world % N", & slen );

After execution, the variable is assigned a value of 11.

7. Commonly Used Escape Character tables in C Language

 
When single quotation marks and backslash are used in character constants and double quotation marks and backslash are used in character constants, escape characters must be used to represent them, that is, the backslash must be added before these characters.
Using escape characters/d Or/x H in C Programs can easily and flexibly express any character. /D is followed by a slash and followed by three Octal numbers. The value of the three Octal numbers is the corresponding octal a s c I code value. /X is followed by two hexadecimal numbers. The two hexadecimal numbers correspond to the hexadecimal a s c I code value of the character.

Note the following when using escape characters:
1) only lowercase letters can be used in escape characters. Each escape character can only be considered as one character.
2) The/V vertical tabulation and/F break have no impact on the screen, but will affect the printer's response.
3) when non-printable characters are used in C Programs, escape characters are usually used.

Note:
1,/V vertical tabulation and/F break do not affect the screen, but will affect the printer to perform the response operation.
2,/N should actually call the carriage return to wrap the line. Line feed only changes one line without changing the horizontal coordinates of the cursor. Press enter to return to the beginning of the line without changing the vertical coordinates of the cursor.
3,/t move the cursor forward four or eight cells, which can be set in the Compiler
4,/'is used in characters (that is, in single quotes. It is not required in the string (that is, double quotation marks). You only need to use.
5 ,/? Actually not necessary. As long as it is used? You can (verify in Windows vc6 and TC2 ).

 

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.