当前位置:网站首页>初识C语言(2)
初识C语言(2)
2022-07-26 22:48:00 【初见于冬】

大家好,我是初识于冬!
前言
接着上回的内容,我将继续给大家分享初阶C语言知识!
五、字符串+转义字符+注释
字符串
"hello bit.\n"
这种由双引号(Double Quote)引起的一串字符称为字符串字面值(String Literal),或者简称字符串。
注:字符串的结束标志是'\0'的转义字符。在计算字符串长度的时候'\0'是结束标志,不算作字符串内容。
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[]="bit";
char arr2[]={'b','i','t'};
char arr3[]={'b','i','t','\0'};
printf("%s\n",arr1); //打印结果为'bit'。
printf("%s\n",arr2); //打印结果为一堆乱码。
printf("%s\n",arr3); //打印结果为'bit'。
printf("%d\n",strlen(arr1)); //打印结果为3。
return 0;
}strlen(string lenth)——>要在头文件加 #include<string.h>
strlen是库函数,是专门求字符串长度的函数(数'\0'之前的字符个数)
转义字符
例:
#include<stdio.h>
int main()
{
printf("c:\code\test.c\n");
return 0;
}
由此可见代码中的'\t'和'\n'没有被打印出来!这里就不得不提一下转义字符了。转义字符顾名思义就是转变意思。
| 转义字符 | 释义 |
|---|---|
| \? | 在书写多个问号时使用,防止他们被解析成三字母词 |
| \' | 用于表示字符常量' |
| \" | 用于表示一个字符串内部的双引号 |
| \\ | 用于表示一个反斜杠,防止它被解释为一个人转义序列符。 |
| \a | 警告字符,蜂鸣 |
| \b | 退格符 |
| \f | 进纸符 |
| \n | 换行 |
| \r | 回车 |
| \t | 水平制表符 |
| \v | 垂直制表符 |
| \ddd | ddd表示1-3个八进制的数字。如\130 (X) |
| \xdd | dd表示2个十六进制数字。 如\x30 0 |
#include<stdio.h>
int main()
{
printf("%c\n",''\'); //打印出来为 '
printf("%s\n","\""); //打印出来为 "
return 0;
}#include<stdio.h>
int main()
{
printf("%d\n",strlen("abcdef")); //打印结果为6
printf("%d\n",strlen("c:\test\628\test.c"); // \62被解析成一个转义字符
retrurn 0;
}#include<stdio.h>
int main()
{
printf("%c\n",'\130'); //打印出来为X
return 0;
}这里为什么会输出X呢?因为将八进制的130转换为十进制为88,88对应ASCII表中的X。

六、注释
为什么代码中需要注释?
- 代码中有不需要的代码可以直接删除,也可以注释掉。
- 代码中有些代码比较难懂,可以加一下注释文件。
注释有有两种风格:
- C语言风格的注释(不能嵌套注释)
- C++语言风格的注释(可以注释一行,也可以注释多行)
代码演示:
#include<stdio.h>
int Add(int x, int y)
{
return x + y;
}
/*C语言风格注释
int Sub(int x, int y)
{
return x + y;
}
*/
int main()
{
//C++的注释风格
//int a=0;
//调用Add函数,完成加法
printf("%d\n", Add(1, 2));
return 0;
}七、选择语句
通过if else语句来实现条件的选择
代码演示:
#include<stdio.h>
int main()
{
int input = 0;
printf("要好好学习吗?(1/0)");
scanf("%d", &input);
if (input = 1)
{
printf("好offer\n");
}
else
{
printf("回家种地\n");
}
return 0;
}八、循环语句
C语言中如何实现循环?
- while语句
- for循环语句(后期详细解释)
- do ......while语句(后期详细解释)
代码演示while语句:
#include<stdio.h>
int main()
{
printf("疯狂写代码\n");
int line = 0;
while (line <= 20000)
{
line++;
printf("我要继续努力敲代码\n");
}
if (line > 20000)
printf("键盘敲烂,月薪两万\n");
return 0;
}兄弟们键盘敲烂,月薪两万!不怕吃苦,努力干!
九、函数
函数的特点是简化代码,代码复用
代码演示:
#include<stdio.h>
int main()
{
int num1 = 0;
int num2 = 0;
int num3 = 0;
printf("输入两个操作数:");
scanf_s("%d %d", &num1, &num2);
num3 = num1 + num2;
printf("%d", &num3);
return 0;
}这段代码略显啰嗦,我们可以通过函数来简化代码!
简化后的代码演示:
#include<stdio.h>
int Add(int x, int y)
{
return x + y;
}
int main()
{
int a = 0;
int b = 0;
printf("输入两个操作数:");
scanf_s("%d %d", &a, &b);
printf("%d",Add(a,b));
return 0;
}
总结
以上就是我给大家总结的初阶C语言干货,若是有错误欢迎宝子们在评论区批评指正哦!也希望我总结的知识能给大家带来收获啦!
边栏推荐
- 24ssh service
- Timestamp conversion Greenwich mean time
- [详解C语言]一文带你玩转选择(分支)结构
- 【volatile原理】volatile原理
- [FPGA tutorial case 29] the second DDS direct digital frequency synthesizer based on FPGA - Verilog development
- MySQL单表查询练习
- Proxmox ve installation and initialization
- IO function of standard C library
- Beyond hidden display ellipsis
- Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (I)
猜你喜欢

The gradient descent method and Newton method are used to calculate the open radical

MySQL主从复制与读写分离

【mysql】mysql启动关闭命令以及一些报错解决问题

云数据库管理初体验

Unity Huatuo example project source code analysis and inspiration
![[FPGA tutorial case 28] one of DDS direct digital frequency synthesizers based on FPGA -- principle introduction](/img/bf/ce4bc33d2a0fc7fe57105e20fbafcf.png)
[FPGA tutorial case 28] one of DDS direct digital frequency synthesizers based on FPGA -- principle introduction

利用九天深度学习平台复现SSA-GAN

Machine learning exercise 7 - K-means and PCA (principal component analysis)

Text to image论文精读GR-GAN:逐步细化文本到图像生成 GRADUAL REFINEMENT TEXT-TO-IMAGE GENERATION
![[daily question] 565. Array nesting](/img/d7/b3fbdbabdc4193816c490b684bba66.png)
[daily question] 565. Array nesting
随机推荐
平面转换(位移、旋转、缩放)
Js九九乘法表
索引失效原理讲解及其常见情况
[FPGA tutorial case 30] DDS direct digital frequency synthesizer based on FPGA -- frequency accuracy analysis with MATLAB
MySQL master-slave replication and read-write separation
Electron FAQ 61 - must the client run with administrator privileges?
In regular expressions (?: pattern), (?! pattern), (? < =pattern) and (?
2022年T2I文本生成图像 中文期刊论文速览-1(ECAGAN:基于通道注意力机制的文本生成图像方法+CAE-GAN:基于Transformer交叉注意力的文本生成图像技术)
MySQL备份恢复
6.28大华笔试
机器学习概述
Unity Huatuo example project source code analysis and inspiration
6.30联发科笔试
MySQL主从复制与读写分离
利用九天深度学习平台复现SSA-GAN
解决方案:炼丹师养成计划 Pytorch+DeepLearning遇见的各种报错与踩坑避坑记录(一)
MySQL view
Freytek central computing platform 360 degree sensing system solves the challenges behind NOA mass production
[daily question] 565. Array nesting
解决方案:读取两个文件夹里不同名的文件,处理映射不对应的文件