Elementary C language (2) - Getting to know C language for the first time

"Wait for the wind to come, it's better to chase the wind!" Today's blog will continue to be updated after the previous blog. If you haven't read the previous blog, you can go to the previous one first. Today will start with the string, let us Let's keep learning together!

Five. String + escape character + comment

1. String

"hello world.\n"

A string of characters enclosed in double quotes is called a string literal, or simply a string.

Note :The end of the string is a \0 escape character, when calculating the length of the string, \0 is only used as a sign of the end, and is not counted as the content of the string.

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
    
    
	//%s是打印字符串的,%c是打印字符的
	char arr1[] = "abc";
	char arr2[] = {
    
     'a','b','c'};
	printf("%s\n", arr1);
	printf("%s\n", arr2);//会显示烫......因为没有出现\0 不可以用%s打印 
	printf("%d\n", strlen(arr1));
	printf("%d\n", strlen(arr2));//会打印出随机值
	//strlen是一个库函数 - 求字符串长度的,统计的是字符串\0之前的字符个数
	return 0;
}

2. Escape characters

Escaping characters, in layman's terms, is to change the original meaning. Next, let's use an example to introduce escape characters. Let's think about it, if we want to print out D:\C\test.c on the screen, how should we write the code?

#include <stdio.h> int main() { printf("D:\C\test.c\n"); return 0; }

According to the conventional thinking, we will write the above code, so, is it right to write it like this? Let's take a look at the results of the operation!
insert image description here
It can be clearly seen that the result is wrong, so why is this? The reason is that there are escape characters in this code. Next, let us see what escape characters are?

#include <stdio.h>
int main()
{
    
    
	printf("abcdnef\n");
	printf("abcd\nef");
	return 0;
}

The difference between the above two lines of code is that a \ is added in front of n, and the result is as follows:

insert image description here
It's easy, we can see that the function of '\n' is a newline.

Next, let's take a look at what escape characters are provided in the C language?

escape character paraphrase
\ ? Used when writing multiple consecutive question marks to prevent them from being parsed into three-letter letters
\ ’ Used to represent character constants'
\ " Used to denote double quotes inside a string
\ \ Used to denote a backslash, preventing it from being interpreted as an escape sequence
\ a warning character, beep
\ b backspace
\ f Feed character
\ n new line
\ r carriage return
\ t horizontal tab
\ v vertical tab
\ ddd ddd represents 1~3 octal numbers. Such as: \130 X
\xdd dd represents 2 hexadecimal digits. Such as: \x30 0

For some of the above vocabulary, you may have doubts, here are some explanations:
1. For the explanation of three-letter words, in many ancient compilers, if you enter the following code on the screen:

int main() { printf("(Are you ok??)"); return 0; }

In the above code, the result you expect is (Are you ok??), but in those ancient compilations, it will print out (Are you ok]. Here, the compiler interprets ??) as], here The ??) is a three-letter word. In order to avoid this phenomenon, we can use the escape character \ ?.

2. For the backspace character \b, give the following example:
insert image description here
We can find that c disappears after using \b, which is the function of the backspace character

3. For the horizontal tab character \t is equivalent to the tab key on the keyboard, move 4 bytes to the right

4. For \ddd, we can understand that it is an octal expression of a character. Given an example, \130 is equal to 0 8 to the 0th power + 3 8 to the 1st power + 1 8 to the 2nd power, and the result is 88, which The corresponding character is 'X'.
We can refer to the following ASCII code table for this result :
ASCII code table
5. For \xdd, we can understand that it is a hexadecimal expression of a character, give an example \x30 is equal to 0
16 to the power of 0+3* 16 to the power of 1, the result is 48, check the ASCII code table, the corresponding character is '0'.

The above two examples can also get the same result by running the code.
insert image description here
Note: For the above ASCII code table, we don’t need to remember it, we can look it up directly, we just need to have an impression of some important ones, such as the ASCII corresponding to the uppercase and lowercase letters, it is not difficult to find that the ASCII of lowercase letters is higher than that of uppercase letters The code value is greater than 32. The characters corresponding to 0~31 in the table are not printable.

3. Notes

1. There are some unnecessary codes in the code that can be deleted directly, or commented out through comments. 2.
Some codes in the code are difficult to understand, and some comment text can be added.
For comments, they are used directly in the above and previous articles However, let's take a look at how to use annotations

  • C-style comments:

/* xxxxxxxxxxxxxxxxxxx */

  • C++ style comments:

// xxxxxxxxxxxx

Alright, that’s all for today. Welcome to follow, like and comment!

Guess you like

Origin blog.csdn.net/qq_73121173/article/details/131349952