当前位置:网站首页>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
- 代码中有不需要的地方可以直接删除,也可以注释掉
- 代码中有些地方会比较难懂,可以加一下注释文字
边栏推荐
- Adding rendererdata of URP with script
- Canvas大火燃烧h5动画js特效
- ZigBee模块无线传输星形拓扑组网结构简介
- Leetcode 929. 独特的电子邮件地址
- Set up an online help center to easily help customers solve problems
- Classic topics of leetcode tree (I)
- This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
- AOV network topology sorting
- c语言---13 循环语句while
- Library for adding progress bar during training --tqdm
猜你喜欢

Why 0.1+0.2=0.3000000000000004

JS special effect of canvas divergent particle H5 animation

YML file configuration parameter definition dictionary and list

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

Canvas大火燃烧h5动画js特效

AI 加持实时互动|ZegoAvatar 面部表情随动技术解析
![[technical analysis] discuss the production process and technology of big world games - preliminary process](/img/44/5404f0da2e17099e89a92e37b2a0cb.png)
[technical analysis] discuss the production process and technology of big world games - preliminary process

IIS安装 部署网站

JS blur shadow follow animation JS special effect plug-in

if else的使用太简单?(看懂这篇你的逻辑会进一步提升)
随机推荐
盛最多水得容器
Detailed explanation of MySQL windowing function
一个WPF开发的打印对话框-PrintDialogX
Some views on the current CIM (bim+gis) industry
One of the Taobao short video pit avoidance Guide Series -- thoroughly understand Taobao short video
第七部分:第二课 顾问通用技能-如何安装和卸载SAP ERP系统客户端
How to locate the hot problem of the game
Why 0.1+0.2=0.3000000000000004
Postman interface test tool
AOV网拓扑排序
Play with pytoch's function class
Solve the problem that vs2022 slowly loads a pile of symbols when debugging the program
线性移动棋
PCA principal component analysis tutorial (origin analysis & drawing, without R language)
淘宝短视频避坑指南系列之一--彻底了解淘宝短视频
(CVPR 2020) RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds
The development of flutter in digital life and the landing practice of Tianyi cloud disk
解决 vs2022在调试程序时缓慢加载一堆符号的问题
Vim常用命令总结
高数_第6章无穷级数__绝对收敛_条件收敛