Zero-based learning of C language (escape characters and strings)

foreword

1. Escape characters

2. String

3. Notes

Summarize

—————————————————————————————————————————
Preface
_ Language), share with you some knowledge and tips about escape characters, strings and comments, as well as some of my understanding.
——————————————————————————————————————

1. Escape character
What is the escape character?

The character set (Character
Set) assigns a unique number to each character, we might as well call it an encoded value. In the C language, a character can be represented not only by its entity (that is, a real character), but also by a coded value. This way of using encoded values ​​to indirectly represent characters is called escape characters (Escape
Character). Escape characters are essentially encoded by ASCII values, but because the ASCII value
range of 0~31 (decimal) will not be directly displayed on our display, nor can it be directly input by keyboard, it can only be represented by escape characters .

In fact, we have seen escape characters a long time ago. In the previous article "First Understanding of C Language", there is an output statement printf("Hello world!\n");in this line of code \n. It is an escape character, which is called a newline character.

escape character paraphrase
\a generate a warning
\b Move the cursor back one space
\f Move the cursor to the first cell of the next page
\n Move the cursor to the first cell of the next line
\r Move the cursor to the first cell of the current line
\t Move the cursor to the next horizontal tab position
\v Move the cursor to the next vertical tab position
\’ produces a single quote
\" produces a double quote
\? generate a hello
\\ produces a backslash
\0 produces a null character
\ddd Indicates 1-3 octal numbers, such as '\123'
\xdd escape character in hexadecimal form followed by up to two digits

Among them \n, and \tare our more commonly used transfer characters, which \nare often used for line breaks, and \tare generally equivalent to 4 spaces.
It should also be noted that the original intention of the escape character is for ASC|| encoding, so the value range of the escape character sum \dddis \xddlimited. Be sure to pay attention to this when doing questions! ! !

The maximum octal value of the octal escape character '\ddd' is '\177' The maximum hexadecimal
value of the hexadecimal escape character '\xdd' is '\x7f'

2. String

The so-called character string generally refers to a sequence composed of multiple characters. A string literal is " "any sequence of characters enclosed in double quotes (including escape characters such as newlines \n). For example:

"Hello world!"

The C language itself does not have a "string" data type. Usually a character array is used to store a string. The difference between a string and an ordinary character array is that there is a null character at the end of the string '\0'. '\0'It is just a sign of the end of the string, not the content in the string, so it will not be counted in the length of the string.

Look at the code below:

#include<stdio.h>
int main(void)
{
    
    
	char str1[] = {
    
     'H','a','h','a','\0' };
	char str2[] = "Haha";
	printf("%s\n", str1);
	printf("%s\n", str2);
	return 0;
}

Soil shoots you
3. Comments
The function of comments:
1. Unneeded parts of the code can be deleted directly, or it can be commented for the next use.
2. Some codes may be understandable when you first write them, but you will always forget them after a long time, so we can write some words to explain them. In this way, not only can we review it ourselves, but it is also convenient for others to understand.
When the program we write becomes more and more complex, many variables are defined. If there is no comment, it will be very difficult for us to come back to read the program after a while; or when we write the wrong program, but it cannot be deleted. The written code can also use comments. The C language provides two comment styles:
when the program we write becomes more and more complex, a lot of variables are defined. If there is no comment, it will be very difficult for us to come back to read the program after a while; or when we write When the program is wrong, but the written code cannot be deleted, comments can also be used. The C language provides two comment styles:

C language comment style: /* xxxxxx*/ Disadvantage: cannot nest comments
C++ comment style: //xxxxx can comment one line or multiple lines

Summary
This article is my understanding of escape characters, strings, and comments. I hope it can help you who are learning C language. I also hope that you can point out my problems and give valuable opinions and suggestions in the comment area. May we make progress together and grow together!

Guess you like

Origin blog.csdn.net/z2004cx/article/details/128145933