Format of C language input/output functions printf and scanf

Source: Internet
Author: User

Format of the C language input/output functions printf and scanf, format rules such as % 5.4f and other similar issues
  The turbo c2.0 Standard Library provides two consoles for formatting Input and Output Functions: printf () and scanf (),

These two functions can read and write data in different formats on the standard input/output device.
   The printf () function is used to write data to a standard output device (screen). The scanf () function is used to input data from a standard input device.

(Keyboard. The following describes the usage of these two functions in detail.
   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. In

This function is often used when programming. 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 output as is; the other

One 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 to be output. The number of parameters must be the same as the number of output parameters described in the formatted string.

Multiple parameters are separated by commas (,), and the order is one to one. Otherwise, unexpected errors may occur.
   
   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.
  Symbol                Function
── ─
   % D            Decimal signed integer
   % U            Unsigned decimal integer
   % F            Floating Point Number
   % S            String
   % C            Single Character
   % P            Pointer Value
   % E            Exponential floating point number
   % X, % x        Unsigned hexadecimal integer
   % 0            Unsigned integer in octal format
   % G            Automatically select the appropriate representation
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.
    Example: % 3d It indicates the number of three integer types to be output. If the number is not three, the right alignment is displayed.
           % 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 It indicates the output string of 8 characters, which is not 8 characters right aligned.
   If the length of a string, or the number of integer digits exceeds the field width, It is output according to the actual length. But for Floating Point

Number. If the integer part of the number of digits exceeds the width of the specified integer part, it is output according to the actual integer part.

After the decimal scale width is specified, 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 4 bits are output, the total width of the value is set to 4 by adding 0 before.
   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, decimal point

The preceding number indicates 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 it is greater than 9, it is 9th characters long

Future content 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 Long Integer
           % Lf Double Floating Point Number output
   (3) You can control the Left or Right alignment of the output, that is, adding a "-" number between "%" and letters indicates the output.

It is left-aligned. Otherwise, it 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.
   Character                         Function
── ─
    \ N                 Line feed
    \ F                 Clear screen and change pages
    \ R                 Enter
    \ T                 Tab character
    \ Xhh               Represents 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, compiles the following program to add

Deep understanding of Turbo c2.0 data types.
   Example 1
    # 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", );   
         Printf ("A = % 6D \ n", );  
         Printf ("A = % 06d \ n", ); 
         Printf ("A = % 2D \ n", );  
         Printf ("* I = % 4D \ n", * I );
         Printf ("* I = %-4D \ n", * I );
         Printf ("I = % P \ n", I );   
         Printf ("F = % F \ n", F );   
         Printf ("F = 6.4f \ n", F ); 
         Printf ("x = % lf \ n", X );  
         Printf ("x = % 18.16lf \ n", X );
         Printf ("c = % C \ n", C );   
         Printf ("c = % x \ n", C );   
      

Related Article

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.