当前位置:网站首页>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
- 代码中有不需要的地方可以直接删除,也可以注释掉
- 代码中有些地方会比较难懂,可以加一下注释文字
边栏推荐
- 如何定位游戏发热问题
- 传统企业在进行信息化升级的过程中,如何做好信息化顶层设计
- Mmdetection build_ Optimizer module interpretation
- [FAQ] summary of common problems and solutions during the use of rest API interface of sports health service
- 盛最多水得容器
- 苹果期待的「无密码时代」,真能实现吗?
- pands pd. Detailed parsing of dataframe() function
- 踩坑了,BigDecimal 使用不当,造成P0事故!
- Abbexa 无细胞 DNA 试剂盒说明书
- CUDA编程(一):实现两个数组相加
猜你喜欢

Detailed explanation of MySQL windowing function

Abbexa 8-OHdG CLIA 试剂盒解决方案

js模糊阴影跟随动画js特效插件

Cdga| six key points of data governance for industrial enterprises

numpy——记录

One of the Taobao short video pit avoidance Guide Series -- thoroughly understand Taobao short video

Canvas fire burning H5 animation JS special effects

Draw confusion matrix

掌握高性能计算前,我们先了解一下它的历史

mmcv之Config类介绍
随机推荐
AI 加持实时互动|ZegoAvatar 面部表情随动技术解析
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
CUDA实现高效查找--审核未通过?
JS special effect of canvas divergent particle H5 animation
How to locate the hot problem of the game
LeetCode树经典题目(一)
IIS installation and deployment web site
LeetCode 321. Maximum number of splices***
安装Linux系统的MySQL,在xshell中遇见的问题
What is image search
用脚本添加URP的RendererData
Numpy - record
Abbexa低样本量鸡溶菌酶 C (LYZ) ELISA 试剂盒
MYSQL开窗函数详解
基于注解和反射生成xml
Aesthetic experience (episode 237) Luo Guozheng
PMP考生,深圳2022年6月PMP考试地点有这些
Talk about those things about telecommuting, participate in the essay solicitation, receive the contribution fee and win the grand prize!
(CVPR 2020) RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds
pands pd. Detailed parsing of dataframe() function