Summary of escape characters and format control issues

Publisher:快乐舞蹈Latest update time:2015-05-05 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
    I have not had time to summarize the recent series of company written tests. Although they are all small companies, there are still many contents worth summarizing. Many of the contents are caused by my failure to read the book carefully. The main problems are format control, escape string and other issues. To be honest, they are all small problems, but if you don't grasp them correctly, they will lead to problems.

 
    Escape characters are a common problem. If I remember correctly, I made several mistakes. I reviewed them carefully tonight. The escape characters that need attention are as follows: , , , , f, \, ', ", ddd, xhh. These escape characters have fixed values ​​in ASCII. It is easy to make mistakes when solving the string length. Each escape character has its own function. It should be noted that in the two escape characters ddd and xhh, the d in ddd is only three numbers from 0 to 7, and the h in xhh can be a character from 0 to 9a to f. However, it should be noted that xhh can be two characters or one character, which requires us to analyze in actual situations. In ddd, it should be noted that the values ​​of the three digits after it cannot be greater than 7.
    For example, the following string:

 

    char Str[]="abc f \'"123789x78xfg";

    The length of the string above is 18. This is a more complicated situation. In this more complicated situation, only by understanding the role of the escape character can the length of the string be calculated. Therefore, we need to consider the problem of escape characters when analyzing the length of the string.

The format of printf, the return value of this function is the number of bytes in the string, this return can be used to implement multiple printing     of the string in sprintf , you can refer to Ruijie's written test questions. There are various formats for printf printing, among which the more easily tested ones are additional format description characters:

    l is used to implement long integers and can be added before the format characters d, o, x, and u.

    m (a positive integer) is the minimum width of the data.

    n (represents a positive integer) For real numbers, it means outputting n decimal places; for strings, it means the number of strings to be truncated.

    - The output numbers or characters are aligned to the left in the field.

    The above four additional formats are often tested in the written test. Although they are small problems, they are the places where we lose points, which shows that our basics are not up to standard. Especially when the string is output in the additional format, it is easy to make mistakes.

 

    %ms, the output string occupies m columns. If the string length is greater than m, the limit of m will be exceeded and the string will be output in its entirety. If the string length is less than m, spaces will be added to the left.

    %-ms, if the string length is less than m, the string will be moved to the left and filled with spaces on the right in the return value of m.

    %m.ns, the output occupies m columns, but only takes n characters from the left end of the string. These n characters are output at the right end of the m column, with spaces padded on the left.

    %-m.ns, where m, n and %m.ns are the same, n characters are output at the left end of m columns, and spaces are added on the right. When n>m, m automatically takes the value of n, that is, n characters are output normally.
    %m.nf, the output data occupies m columns, with n decimal places. If the data length is less than m, spaces are added on the left.
    %-m.nf and %m.nf just make the output value lean to the left end and fill spaces on the right end.

   It should also be noted that the percent sign is printed using "%%".  

   The format setting of scanf is similar to that of printf, but there are also some differences. scanf cannot limit the width of the decimal point, only the width of the bit field, and no n value. You also need to pay attention to the existence of an ignore symbol "*", such as %*3d, which ignores the 3-digit integer and skips the assignment to the variable.

   l is used to input length-shaped data

   h is used to input short integer data

   field width specifies the width of the input data. The field width is a positive integer.

   * Indicates that this input item will not be assigned to the corresponding variable after being read.

    Today I will briefly summarize the basic format. Although these are the most basic knowledge, this is all you can do for a written test.

Reference address:Summary of escape characters and format control issues

Previous article:The difference between kernel logical address and kernel virtual address
Next article:Application of accompanying array and counting sort

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号