当前位置:网站首页>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
边栏推荐
- Power consumption development experience sharing: design power consumption board
- A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
- Explain in detail the arithmetic operators related to matrix operation in MATLAB (addition, subtraction, multiplication, division, point multiplication, point division, power)
- Redis cache penetration
- Calculus review 1
- ^29 event cycle model
- Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
- app測試用例
- Theoretical basis of distributed services
- Redis缓存击穿
猜你喜欢

^30h5 web worker multithreading

Self attention and multi head attention

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

Pytorch deep learning -- neural network convolution layer conv2d

pytorch深度学习——卷积操作以及代码示例

信号与系统复习1

2 pcs share a set of keyboard and mouse

详解MATLAB中与矩阵运算有关的算术运算符(加、减、乘、除、点乘、点除、乘方)

Will your company choose to develop data center?

User defined date component. The left and right buttons control forward or backward year, month, week and day turning
随机推荐
Unity analyzes the rendering of built-in terrain and does some interesting things
Redis cache breakdown
LeetCode 进阶之路 - 加一
Leetcode advanced path - reverse string
LeetCode:497. Random points in non overlapping rectangles -- medium
标配双安全气囊,价格屠夫长安Lumin 4.89万起售
H.264中NALU、RBSP、SODB的关系
A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
Connexion MySQL errorcode 1129, State hy000, Host 'xxx' is Blocked because of many Connection Errors
Record today's MySQL failures
Vissim仿真快速入门
Power set V4 recursion of brute force method /1~n
Redis cache penetration
2021年平均工资出炉,IT行业不出所料
Identity and access management (IAM)
防抖和节流
Practical | how to use burp suite for password blasting!
分布式服务理论基础
The programmed navigation route jumps to the current route (the parameters remain unchanged), and the navigationduplicated warning error will be thrown if it is executed multiple times?
Signal and system review 1