19.C language formatted output

A, formatted output

There are formatted output function printf, sprintf and snprintf, etc., function slightly differently, using much the same way, this section we first printf example.

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 end of the first format string
description of a format corresponding to a character output parameter, The second format specifier corresponding to the second output parameter, the third
th 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, the column is not unusual.

% 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.

Knowledge of the above output data at the time of presentation before the data type of presentation has been, there is not an example of.

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.

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

**: - ** content output left-justified.

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=

Second, the formatted output string

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

Function: 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, not n characters.

In the previous section, introduced convert string library functions integer and floating point data, C language does not provide the integer and floating point data is converted into a string library functions, instead of using the output formatting functions sprintf and snprintf to string.

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.

Third, the multiple lines of code written in C language

In the process we learned before, the preparation of the functions of the program is very simple, a code is very short, but in the actual development, a lot of parameters are often very long, a code can be very long, you need to use multiple lines to write.

If we place a backslash end of the line one line of code, c language compiler ignores newline end of the line, while the contents of the next line is also counted as the contents of the Bank. Here backslash played a role in the continued line.

strcpy(str,"aaaaaaaaaa\
bbbbbbbbb);

If we do not use the backslash, when we try to initialize a string across multiple lines, c language compiler might issue a warning or error, as the following statement is not correct.

strcpy(str,"aaaaaaaaaa
bbbbbbbbb);

C language string method there are multiple lines of writing that it will write multiple strings, C language compiler will automatically connect these strings together, as follows:

 strcpy(str,"aaabbbccc");
  printf("str=%s=\n",str);   // 输出str=aaabbbccc=
 
  strcpy(str,"aaa""bbb""ccc");
  printf("str=%s=\n",str);   // 输出str=aaabbbccc=

  strcpy(str,"aaa"\
             "bbb"\
             "ccc");
  printf("str=%s=\n",str);   // 输出str=aaabbbccc=


  sprintf(str,"aaabbbccc");
  printf("str=%s=\n",str);   // 输出str=aaabbbccc=

  sprintf(str,"aaa""bbb""ccc");
  printf("str=%s=\n",str);   // 输出str=aaabbbccc=

  sprintf(str,"aaa"\
              "bbb"\
              "ccc");
  printf("str=%s=\n",str);   // 输出str=aaabbbccc=

The output above each code is exactly the same expression: "aaa" "bbb" " ccc" is actually equivalent to
"aaabbbccc".

The string is very long, a lot of arguments with multiple lines of code to write, can make the program code structure clearer, the following code is a code that I used in the actual development, which is not too long.

Here Insert Picture Description

Fourth, homework

1) Write the sample program, this section describes the knowledge of all the demo again, the demo program can deepen your understanding and mapping.

2) Write a function of parsing XML string.

Function declaration:

int  GetXMLBuffer(const char *in_XMLBuffer,const char *in_FieldName,char *out_Value);

String in_XMLBuffer, XML format, as follows:

<name>西施</name><age>18</age><sc>火辣</sc><yz>漂亮</yz>

in_FieldName, tag name field.

out_Value, get a pointer to the content stored in the variable.

Return value, 0 to the success, failure -1-.

Call:

char strXMLBuffer[1024];
strcpy(strXMLBuffer," <name>西施</name><age>18</age><sc>火辣</sc><yz>漂亮</yz>");
char strvalue[51];
GetXMLBuffer(strXMLBuffer,"name",strvalue);   // strvalue的内容将是"西施"
GetXMLBuffer(strXMLBuffer,"age",strvalue);     // strvalue的内容将是"18"
GetXMLBuffer(strXMLBuffer,"sc",strvalue);       // strvalue的内容将是"火辣"
GetXMLBuffer(strXMLBuffer,"yz",strvalue);       // strvalue的内容将是"漂亮"

V. 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: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published 29 original articles · won praise 2 · Views 676

Guess you like

Origin blog.csdn.net/m0_45133894/article/details/104648662