C language output format

printf (format control, output table column)

 

For example: the printf ( " I =% D, CH = C% \ n- " , I, CH);

 

Description:

   (1) "format control" is enclosed by a double prime character string, also known as "switching control string", which includes two types of information:
         Format Description ①: the "%" character and format, its role is to convert the output data to the specified output format.
         ② ordinary characters, that character is output needs.
   (2) "Output Table column" are some of the data required to be output, the expression may be
   (3) The general form of the printf function can be expressed as
         printf (parameter 1, parameter 2, ......, parameter n)
         2 is a functional parameter parameter n ~ 1 Parametric given output format
1. Conversion specifier
% a (% A) floating-point number, hexadecimal numbers and p- (P-) notation (the C99)
% C character
% d signed decimal integer
% f float (including float and doulbe)
% e (% E) floating point exponent output [e- (E-) notation]
% G (G%) float insignificant insignificant zero "0"
% signed decimal integer I (the same as% d)
% u unsigned decimal integer octal integer% O
% x (% X) hexadecimal integer 0F (0F) EG 0x1234
% P pointer
% s string
%% output character%
2. Flag
Left: "-" such as: "% - 20s"
Align Right: "+" For example: "% + 20s"
box: If the sign is positive, the display space, negative display "-" For example: "% 6.2f "      
#: for c, s, d, u class no effect; o based on, when the output o prefix; based on x, when the output prefix 0x; of e, g, f category when only fractional results given decimal point.
printf full format of the format of the control:
% - Mn 0 h L format or the character 
of the composition of the format described below will be described:
①%: indicating a start symbol format described indispensable.
②-: Yes - represents a left-aligned output, such as a right-aligned output are omitted.
③0: 0 0 indicates that the specified gap fill, such as fill not assigned slot are omitted.
④m.n: m refers to the wide-field, i.e., the number of entries corresponding to the character output on the output device occupied. N refers to the accuracy. Real numbers for explaining the output of decimal places. When n is not specified, an implicit precision of n = 6 bits.
⑤l or h: l refers to long integer type, double type of real finger. h for the character format for the short integer type of correction. Format character  format character data type for designating the output format and output items. ①d format: for outputting decimal integer. There are several uses: % D: output according to the actual size of the integer data. % md: m output field specified width. If the number of data is smaller than m, to fill the spaces left, if greater than m, at the actual number of bits output. % ld: long data output. ②o format: unsigned octal integer output. Long can be used for "% lo" output format. The same can also be specified with the field width "% mo" output format. Example: main () {int A = -1; the printf ( "% D,% O", A, A);} The result: -1,177777 program analysis: 1 in the memory unit (stored in two's complement form ) is (1111111111111111) 2, octal number (177 777) 8.
 


 










③x formats: unsigned integer output in hexadecimal form. Long can be used for "% lx" output format. The same can also be specified with the field width "% mx" output format.
④u formats: unsigned integer decimal output. It may be "% lu" output format for a long integer. The same can also be specified with the field width "% mu" output format.
⑤c format: output a character.
⑥s format: a string to output. There are several usage
% s: example: printf ( "% s", "CHINA") outputs "CHINA" string (without quotation marks).
% ms: m columns representing the output string, the string itself as a length greater than m, m is eligible to break out, the entire output string. If the string length is less than m, then fill the spaces left.
% -ms: If the string length is less than m, m is in the range of the column, by the string left, up and right spaces.
% m.ns: m columns representing output, but only to take a string of n characters left. This output characters to the right of n and m columns, fill the space left.
% -m.ns: wherein m, n are as above, output of n-characters in the left column of the range of m right up space. If n> m, n values are automatically taken, which is to ensure the normal output n characters.
⑦f format: for outputting a real number (including single, double), the output of a decimal. There are several uses:% f: the width is not specified, and outputs the integer part of the total output 6 decimal places.
% m.nf: output accounted for m columns, where n has a decimal places, such as the value m is smaller than the width of the left end of the meeting spaces. % -m.nf: output accounted n columns, where n has a decimal places, such as the value m is smaller than the width of the right end of the meeting spaces.
⑧e formats: Real output exponentially. Available in the following forms:
% E: the digital part (known as a mantissa) 6 outputs fractional exponent portion comprises four or five. % m.ne and% -m. ne: m, n, and "-" characters the same meaning as before. Where n denotes a decimal portion of the digital data, m represents the whole width of the output data occupied.

⑨g format: f format or automatic selection of a shorter e output format, does not output the zero meaningless.
---------------------------------------
Further information on the printf function:
If you want to output characters "%", two consecutive% should be indicated by a "format control" string, such as:
the printf ( "% F %%", 1.0 /. 3);
output 0.333333%.
For single precision number, using% f formatter output is valid only the first 7 digits, fractions 6.
For doubles,% lf using output formatter, the first 16 bits are valid numbers, decimal 6.
For mn format may also use the following method represents a char CH [20 is];
the printf ( "% * * S \ n-.", M, n-, CH);
front * definition is the total width, behind * defined is the number of outputs. Respectively outside parameters m and n. I think the benefits of this approach is that the parameters m and n can be assigned in addition to the statement, so as to control the output format. Today (06.6.9) also see the output formats% n value can be assigned Dai a variable length string is outputted, for example below: int sLen; the printf ( "Hello World% n", & sLen); performed after variable is assigned to 11. Output integer output 8 bytes (64 bits) When the format should% llu% lld% llo like; int main (int argc, char * the argv []) {   time_t sec = 9223372036854775807;
 



 

 



  printf("time = %lld\n" , sec);
  printf("time = %llx\n" , sec);
  printf("time = %0*llx\n" , 2 * sizeof(time_t) , sec);
}
输出:
 time = 9223372036854775807
 time = 7fffffffffffffff
   time = 7fffffffffffffff

Guess you like

Origin www.cnblogs.com/a-s-m/p/10992534.html