C language printf function Detailed

[Heel]

C language function has formatted output printf, sprintf snprintf and the like, a slightly different function, using similar methods, described herein as an example to introduce the printf function to their use.

For the printf function, I believe we are not unfamiliar. The reason why the formatted output function call, the function fame is as follows:

int printf(const char *format, ...);

We see the statement printf function will be a bit ignorant, it is written with the parameters of what we've learned before the function is not the same, the printf function is a "variable parameter function" (ie, the number of arguments is variable), knowledge of the variable parameters of the function later, and now just know how to use on the line.

The number and type of parameters are variable printf function, the output format of each parameter has a corresponding format specifier corresponding thereto, from the left format string description of the first symbols correspond to a first format output parameters, The second format specifier corresponding to the second output parameter, the third format specifier corresponding to the third output parameter, and so on.

Wherein the format specifier follows the form (in square brackets [] in the entry is optional):

   %[flags][width][.prec] type

1, character type (type)

It is used to indicate the type of output data, the following is a summary of the type commonly used, not commonly used not introduced.

% Hd,% d,% ld decimal, integer output symbol has the form of short, int, long type.

% Hu,% u,% lu decimal, unsigned integer output in the form of short, int, long type

% C output characters.

% Lf output in a conventional manner double (float deprecated, long doube useless).

% E double output in scientific notation.

% S output string.

2, the width (width)

It is used to control the width of the output contents.

   printf("=%12s=\n","abc");     // 输出=         abc=
   printf("=%12d=\n",123);       // 输出=         123=
   printf("=%12lf=\n",123.5);    // 输出=  123.500000=

3, alignment marks (the flags)

flags for controlling the alignment of the output contents.

Do not fill or + : the content right output alignment, which is the default mode, the previous section is an example of a right-aligned.

- : left-justified output content.

   printf("=%-12s=\n","abc");    // 输出=abc         =
   printf("=%-12d=\n",123);     // 输出=123         =
   printf("=%-12f=\n",123.5);    // 输出=123.500000  =

If the content output is integer or floating point, and aligned to right justify, zero padding may be added, for example:

   printf("=%012s=\n","abc");  // 输出=         abc=
   printf("=%012d=\n",123);   // 输出=000000000123=
   printf("=%012f=\n",123.5);  // 输出=00123.500000= 

From the above results, the first line of code, not the contents of the output integer or floating point, string, can not fill the front 0.

Left-justified when it can fill 0 in the back of integer or floating point? Float up to a maximum of six, integer not, why? You can deposit up 0 it later?

4, precision (prec)

If the content of the output is floating point, which is used to control the output accuracy of the contents, that is to say the number of reserved bits after the decimal point, the number is rounded back.

   printf("=%12.2lf=\n",123.5);   // 输出=      123.50=
   printf("=%.2lf=\n",123.5);     // 输出=123.50=
   printf("=%12.2e=\n",123500000000.0);  // 输出=    1.24e+11=
   printf("=%.2e=\n",123500000000.0);    // 输出=1.24e+11=

5, formatted output to a string

int printf(const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);

printf is output to the screen, sprintf formatted output to save the contents of the string str, similar to the snprintf strncpy the n-n, meaning that only the output result before obtaining n-1 characters, instead of n characters .

C language library functions provided to convert the string to integer and floating point data library functions, but without the integer and floating point data is converted into a string, instead of using sprintf and snprintf function into a string formatted output.

Example (book98.c)

/*
 * 程序名:book98.c,此程序演示格式化输出sprintf和snprintf函数。
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>
#include <string.h>

int main()
{
   char str[301];

  // 格式化输出到str中
  sprintf(str,"%d,%c,%f,%s",10,'A',25.97,"一共输入了三个数。");
  printf("%s\n",str);

  // 格式化输出到str中,只截取前7个字符
  snprintf(str,8,"%d,%c,%f,%s",10,'A',25.97,"一共输入了三个数。");
  printf("%s\n",str);
}

operation result Here Insert Picture Description

The second line running output only six characters, note, snprintf function performance under unix and windows platforms is slightly different, in the windows platform, the second line will output 7 characters.

6. Copyright

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.

Source: C Language Technology Network ( www.freecplus.net ) Author: agricultural Ethics Code if this article helpful to you, please support praise, or forwarded my article in your blog, thank you! ! !

Guess you like

Origin www.cnblogs.com/wucongzhou/p/12668766.html