C Programming (2) - format control characters, the standard input and output functions

Identifier (Identifier): begin with a letter or underscore, letters, digits, and the underscore, case sensitive.

Format control characters:

With the input-output function scanf () and printf () format of the control section, i.e. double quotes before the first comma, to output data (identifiers or expressions) format description, including data types, length, alignment, scale and the like.

 1. The format control characters in the form of

% [-] [0] [m / mn] [l / h] as format

     Wherein the content of [] which are optional:

     -: indicates left justified, right justified by default (if not aligned with a predetermined width of the output data is compared with the left);

     0: when the output data of a predetermined width, the actual width of the output data less than a predetermined width at the left with "0" are filled;

     m / mn: m specifies the width of the output data (bits), if the data is greater than the actual width of m, m is a breakthrough limit full output, on the contrary, after the output data of all filled with a space left; n is a real number for refers to the number of digits after the decimal point, if the actual number of digits after the decimal point is less than n, rounded-off press interception; for the string, n represents the number of characters, taken from left to right;

     l / h: l represents long integer data (available for% ld,% lo,% lx,% lu), and a double-data (% lf or% le); h represents a short integer data (available for% hd, % ho,% hx).

2. Its format effector functions (output example)

  % D: decimal for output signed integer (including char type)

  % U: output for unsigned decimal integer (including char type)

  % O: output for unsigned integer octal

  % X: for outputting the unsigned integer hexadecimal

  % C: is used to output a single character

  % S: string to output a

  % F: fractional output for decimal floating-point numbers (in decimal form and the input exponent can be identified)

  % E: for decimal floating-point exponential output (and input a decimal exponent can be identified)

  % G: for outputting both the exponent and fractional decimal floating point short (input and decimal exponent can be identified)

3. Notes

  (1) f precision floating-point represented by 6, if the actual bit floating decimal length greater than 6, the interception 6, 7 rounding bit; if the actual floating point decimals less than 6, the trailing zero. The converted into 1.23 1.230000; 1.234568 1.23456789 converted;

  (2) for a real number, m means the number of bits of data comprises decimal point;

  (3)% as arithmetic operators, can be used for obtaining the remainder (also called modulo), the same sign as the dividend remainder.

4. Examples

 

Stdio functions:

  Appear ordinary character input: enter the full;

  Score Input: {scanf ( "% d /% d", & x, & y); z = x / y;};

  Interception (to enter the ID number taken Date of Birth Example): {scanf ( "% * 6d% 4d% 2d% 2d% * 4d", & i, & j, & k); printf ( "% d% d%.. d \ n ", i, j, k);};

  Example:

   

    

  

 

 

 

  

 

Guess you like

Origin www.cnblogs.com/zimsky/p/12634354.html