当前位置:网站首页>C language ---5 initial string, escape character and comment
C language ---5 initial string, escape character and comment
2022-06-10 21:29:00 【Try!】
1、 character string
“hello\n” This is made up of double quotation marks (double quote) A string of characters is called a string literal (string literal), Or abbreviated string . The end of a string is a “\0” The escape character of , When calculating the length of a string ,“\0” It's the end sign , It doesn't count as string content .
#include <stdio.h>
int main()
{
char arr1[] = "hello";// The size of the array is not written here , The array initializes it based on the contents of the following string
return 0;
}
After writing this code , Press on the keyboard F10 Start debugging , Click on the menu bar “ debugging ”–“ window ”–“ monitor ”, You can see the contents of the array :
Modify the code , View the run results :
int main()
{
char arr1[] = "hello";
char arr2[] = {
'h','e','l','l','o'};
printf("%s\n",arr1);
printf("%s\n", arr2);
return 0;
}

Because when the program is running , Is for array 1 And arrays 2 Memory space is allocated separately , For arrays 1 Come on , After printing the string hello when , It has an end identifier ; But for arrays 2 Come on , There is no identifier for adding the end manually , So after printing hello after , Will find the end identifier in memory , Stop running whenever you find it , When the end identifier is not found, the contents in the memory will be printed , And we don't know what's in the memory , So there will be garbled code . We “ monitor ” Let's take a look at the debugging process :
Based on this code , Let's see if the two arrays have the same length ?
int main()
{
char arr1[] = "hello";
char arr2[] = {
'h','e','l','l','o' };
printf("%s\n",arr1);
printf("%s\n", arr2);
printf("%d\n",strlen(arr1));
printf("%d\n", strlen(arr2));
return 0;
}
The operation results are as follows : The length of the first array is 5, The second is a random value . Because in the array 2 in , We don't know ’h’,‘e’,‘l’,‘l’,'o’ What else will follow ,strlen Will keep looking for the end identifier , It won't stop until you find it .
hello
hello Hot, hot, hot, hot, hot ?p.
5
34
take char arr2[] = {'h','e','l','l','o'}; Change it to char arr2[] = {'h','e','l','l','o','\0'};, Add the end identifier manually , Look at the results , And monitor :

in addition , I have encountered such problems in my study , When monitoring, it will show that the memory cannot be read , Not very good at solving , I hope you can help me ~

2、 Escape character
| \? | Use... When writing multiple consecutive question marks , Prevent them from being parsed into three letter words |
|---|---|
| \ ’ | Used to represent character constants ’ |
| \" | Double quotation marks used to represent the inside of a string |
| \ | Used to indicate a backslash , Prevent it from being interpreted as an escape sequence character |
| \a | Warning characters , Beep |
| \b | Back space |
| \f | Paper in |
| \n | Line break |
| \r | enter |
| \t | Horizontal tabs |
| \v | Vertical tabs |
| \ddd | ddd Express 1`3 Eight octal digits , Such as \130 Express X |
| \xdd | dd Express 2 Hexadecimal numbers , Such as \x30 Express 0 |
int main()
{
printf("%c\n",'\130');
printf("%c\n", '\101');
printf("%c\n", '\x30');
return 0;
}
Running results :
X
A
0
Octal 130 Expressed as decimal is :1x82+3x81+0x80=88, Capitalization X Of ASCII The code value is 88, So the print result is X. octal 101 The corresponding decimal system is 65,65 Corresponding ASCII The code value is A, So the print result is A.
x30 Expressed as decimal is :3x161=48,48 Corresponding ASCII The code value is a character 0.
practice :
Think about the output of the program ?
int main()
{
printf("%d\n",strlen("abcdefg"));
printf("%d\n", strlen("c:\test\328\test.c"));
return 0;
}
The result is 7 and 14. The second reason is 14 Well , because 
Be careful :\328 here \32 Is part of ,8 Is part of , Because there is no three digit octal 8(0-7).
3、 notes
Shortcut key :ctrl+c+k,ctrl+k+u
- There are unnecessary places in the code that can be deleted directly , You can also comment out
- Some parts of the code are difficult to understand , You can add the annotation text
边栏推荐
- H.264中NALU、RBSP、SODB的关系
- 保姆级教程:如何成为Apache Linkis文档贡献者
- Nodejs: official document 3 Dgram stream
- Leetcode advanced road - 125 Validate palindrome string
- "O & M youxiaodeng" self service account unlocking tool
- 堆排序与加强堆代码 用于记忆
- Record today's MySQL failures
- 01js basic null and undefined difference type conversion = = code block logical operator
- app测试用例
- LeetCode 进阶之路 - 删除排序数组中的重复项
猜你喜欢

连接mysql报错 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors

Redis cache breakdown

简解深度学习Attention

Software definition boundary (SDP)

synergy: server refused client with our name

数据库系统概论 ---- 第一章 -- 绪论(重要知识点)

Understanding deep learning attention

Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?

Can you still have a wonderful life if you are laid off at the age of 35?

CET-6 - Business English - the last recitation before the test
随机推荐
自媒体视频创作如何选择领域?
Extracting Nalu unit data from H264 real-time stream
Construction of RT thread smart win10 64 bit compilation environment
^30h5 web worker multithreading
Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)
Naturalspeech model synthetic speech achieves human speech level for the first time in CMOS test
synergy: server refused client with our name
H265 Nalu type judgment and SPS data analysis
Will your company choose to develop data center?
[computer use] how to set software startup without auto startup
设计多层PCB板需要注意哪些事项?
A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
Theoretical basis of distributed services
^29 event cycle model
20192407 2021-2022-2 experimental report on Experiment 8 of network and system attack and Defense Technology
在手机上买基金安全吗?会不会被吞本金?
蛮力法/u到v是否存在简单路径
Pytorch deep learning -- convolution operation and code examples
app测试用例
72. 编辑距离 ●●●