当前位置:网站首页>c语言---5 初识字符串、转义字符、注释
c语言---5 初识字符串、转义字符、注释
2022-06-10 17:31:00 【要努力丫!】
1、字符串
“hello\n”这种由双引号(double quote)引起来的一串字符称为字符串字面值(string literal),或简称字符串。字符串的结束标志是一个“\0”的转义字符,在计算字符串长度的时候,“\0”是结束标志,不算作字符串内容。
#include <stdio.h>
int main()
{
char arr1[] = "hello";//这里不写数组的大小,数组就会根据后面字符串的内容来初始化它
return 0;
}
写完这段代码之后,按键盘上的F10启动调试,点击菜单栏的“调试”–“窗口”–“监视”,就可以看到该数组的内容:
修改代码,查看运行结果:
int main()
{
char arr1[] = "hello";
char arr2[] = {
'h','e','l','l','o'};
printf("%s\n",arr1);
printf("%s\n", arr2);
return 0;
}

因为在程序运行的时候,是给数组1以及数组2分别分配了内存空间,对于数组1来说,打印完字符串hello时,它有结束标识符;但是对于数组2来说,没有手动加入结束的标识符,所以打印完hello之后,就会在内存中找结束标识符,啥时候找到就啥时候停止运行,在没找到结束标识符的时候就会打印内存里面的内容,而我们也不知道内存里面是什么内容,所以会有乱码出现。我们“监视”一下调试过程来看一下:
在此代码的基础上,我们来看一下两个数组的长度是否一样?
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;
}
运行结果如下:第一个数组的长度为5,第二个为随机值。因为在数组2中,我们不知道’h’,‘e’,‘l’,‘l’,'o’后面还有什么内容,strlen就会一直去寻找结束标识符,直到找到才会停止。
hello
hello烫烫烫烫烫烫烫烫烫烫烫?p.
5
34
将char arr2[] = {'h','e','l','l','o'};改为char arr2[] = {'h','e','l','l','o','\0'};,手动加上结束标识符,看运行结果,并作监视:

另外,在学习中碰到这样的问题,就是监视时会显示无法读取内存的情况,不太会解决,希望有会的伙伴帮助一下~

2、转义字符
| \? | 在书写连续多个问号时使用,防止它们被解析成三字母词 |
|---|---|
| \ ’ | 用于表示字符常量’ |
| \" | 用于表示一个字符串内部的双引号 |
| \ | 用于表示一个反斜杠,防止它被解释为一个转义序列符 |
| \a | 警告字符,蜂鸣 |
| \b | 退格符 |
| \f | 进纸符 |
| \n | 换行 |
| \r | 回车 |
| \t | 水平制表符 |
| \v | 垂直制表符 |
| \ddd | ddd表示1`3个八进制数字,如\130表示X |
| \xdd | dd表示2个十六进制数字,如 \x30 表示0 |
int main()
{
printf("%c\n",'\130');
printf("%c\n", '\101');
printf("%c\n", '\x30');
return 0;
}
运行结果:
X
A
0
八进制的130表示为十进制是:1x82+3x81+0x80=88,大写X的ASCII码值为88,故打印结果为X。八进制101对应的十进制是65,65对应的ASCII码值为A,故打印结果为A。
x30表示为十进制是:3x161=48,48对应的ASCII码值是字符0.
练习:
想一下程序的输出结果会是多少?
int main()
{
printf("%d\n",strlen("abcdefg"));
printf("%d\n", strlen("c:\test\328\test.c"));
return 0;
}
结果是7和14.第二个为什么是14呢,因为
注意:\328这里\32是一部分,8是一部分,因为三位数的八进制里面没有8(0-7)。
3、注释
快捷键:ctrl+c+k,ctrl+k+u
- 代码中有不需要的地方可以直接删除,也可以注释掉
- 代码中有些地方会比较难懂,可以加一下注释文字
边栏推荐
- 2022 version of idea graphical interface GUI garbled code solution super detailed simple version
- THE LOTTERY TICKET HYPOTHESIS: FINDING SPARSE, TRAINABLE NEURAL NETWORKS论文笔记
- Why 0.1+0.2=0.3000000000000004
- 第七部分:第二课 顾问通用技能-如何安装和卸载SAP ERP系统客户端
- 基于业务沉淀组件 =&gt; manage-table
- canvas发散的粒子h5动画js特效
- One of the Taobao short video pit avoidance Guide Series -- thoroughly understand Taobao short video
- Vim常用命令总结
- 微信小程序仿陶票票课程设计
- Abbexa丙烯酰胺-PEG-NHS说明书
猜你喜欢
随机推荐
Generate XML based on annotations and reflection
XML&Xpath解析
numpy——记录
字符串的分析和使用 上
高数_第6章无穷级数__绝对收敛_条件收敛
Mmdetection build_ Optimizer module interpretation
Leetcode 875. Coco, who likes bananas
Classic topics of leetcode tree (I)
The short ticket hypothesis: finding sparse, trainable neural networks
True thesis of information system project manager in the first half of 2022
LeetCode 255. Verifying preorder traversal sequence binary search tree*
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
Some views on the current CIM (bim+gis) industry
JS special effect of canvas divergent particle H5 animation
There is an urgent need to enrich the smart home product line. Can fluorite be crowded on the sweeping robot track?
c语言---12 分支语句switch
Abbexa 8-OHdG CLIA 试剂盒解决方案
Leetcode 929. 独特的电子邮件地址
美学心得(第二百三十七集) 罗国正
Summary of all contents of cloud computing setup to ensure that a complete cloud computing server can be built, including node installation, instance allocation, network configuration, etc

![[technical analysis] discuss the production process and technology of big world games - preliminary process](/img/44/5404f0da2e17099e89a92e37b2a0cb.png)







