Detailed explanation of escape characters in C language

Detailed explanation of escape characters in C language

Introduction

Characters, generally after typing on the keyboard, the directly displayable characters become displayable characters .
However, there are some characters that do not have a corresponding key on the keyboard. Most of these characters are designed for control and are called control characters. If you want to type control characters, you need to use escape characters. It is to use characters beginning with "\".

Escape character

In the C language, there are not many types of escape characters, and there are probably these:

  • \a → bell

  • \f → change page

  • \n → newline

  • \0 → end of character string

  • \r → carriage return (return to the beginning of the current line)

  • \b → backspace

  • \t → Horizontal tab

  • \v → vertical tab

There is nothing to say about the first few of them. It is worth mentioning that the meaning of \b is backspace, which means to move the cursor to the previous unit . For example printf("123\b45");, it will print 1245 after it, but printf("12345\b");it will still print 12345 at time. Because \b only moves the cursor from behind 3 Moved to the front of 3, after the first output function, and then printed 45, 3 was overwritten, so it looks like deleted, but when \b is placed at the end, because only after the cursor is moved Nothing is done, and it looks like it was not deleted. For example, the following code:

 char a = 'a', b = 'b', c = 'c';
 printf("%c %c %c\n",a,b,c);
 printf("%c%c\b%c\n",a,b,c);
 printf("%c%c%c\b\n", a, b, c);
 printf("123\b45");

The results of the operation are as follows:
alt
and the following \v is a vertical tab, meaning that the input is aligned with the text position after line break. Just like a table. But it is useless in the command line window, generally only used for text editing. \t is a horizontal tab, which is expressed as pressing a Tab, just like a long space; in fact, in the text, the space is also regarded as a character, and the tab key is the space we usually understand.

Print transfer characters

When we are writing code, we may encounter some situations when we originally didn’t want to use escape characters, but the system recognizes them as escape characters; for example, we want to print: D:\data\text\main.c at this time we will find Printing will become D:data extmain.c. At this time, you need to add an extra backslash to print a slash. Similarly, if we want to print escape characters, we need to type an extra backslash. For example, if we want to print \n, we need Enter \n. At this time, the first two backslashes will be judged as printing a backslash character, and will not be associated with the following characters to form an escape character. So our code should be changed to:
`printf("D:\data\text\main.c"); The
output is as follows:
Insert picture description here

Other escape characters

In the C language, all characters can be represented by "+ASCII code value."

  • '\x'+hexadecimal number dd
  • '\'+octal number ddd

Wherein dd is the number of bits that can be used, if exceeded will cause an error to print characters, such as the ASCII code is a 97, can be used:
the printf ( "\ X61");
the printf ( "\ 141 is");
print the results as follows :

Insert picture description hereIt should be noted here that decimal numbers cannot be input, and not every octal or hexadecimal number can be converted into a character in this way. Because the ASCII code value we know is represented by decimal. But some octal or hexadecimal numbers will have a decimal part when converted to decimal numbers. These octal numbers and hexadecimal numbers cannot be converted into characters.

Guess you like

Origin blog.csdn.net/sy1227081317/article/details/109316558