2024-05-29 day 9

1.函数

1.1递归函数

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n913" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">1.概念: 一个函数在函数体内又调用了本身,我们称为递归调用,这样的函数称为递归函数。
2.递归函数成功执行需要满足以下两个条件:
(1)必须有一个明显结束条件
(2)必须有一个趋于结束条件的趋势。</pre>

1.2 常用系统函数

在线手册地址: https://zh.cppreference.com/w/c/header

①字符串函数

标准库头文件<string.h>

函数 说明
strlen(str) 返回str的长度(size_t类型)
strcpy(str) 将str2中的字符串复制到str1中(给str1重新赋值)
strcat(str1,str2) 将str2中的字符串追加到str1后面

标准库头文件<stdio.h>

函数 说明
sprintf() 用于将格式化数据写入字符串,相比于printf,多了一个参数,第一个参数是要写入的字符串名字,后面参数与printf()一致。简单来讲,sprintf()是将内容写入字符串,而不是输出。
sscanf() 用于从一个字符串中按照指定的格式提取数据,相比于scanf(),多了一个参数,第一个参数是要提取数据的字符串,后面参数与scanf()一致。简单来讲,sscanf()是从字符串中提取数据而不是从用户输入中提取数据。

② 日期时间函数

标准库头文件 <time.h>

函数 说明
time(&变量) 获取当前日期赋值到变量中,该变量需是 time_t 类型
ctime(&时间值) 将时间戳转为字符串并返回,时间值需是 time_t 类型
difftime(时间值1,时间值2) 返回两个时间值的差,返回值是 double 类型,时间值需是 time_t 类型

time_t 是C语言中用于表示时间值的数据类型,它通常是一个整数类型(int ``或 long 或 long long),用于存储时间戳。

时间戳是指从1970年1月1日(通常称为UNIX纪元)零时零分零秒(UTC时间)起至某一特定时刻所经过的秒数。

③ 数学计算函数

标准库头文件 <stdio.h>

函数 说明
sqrt(x) 计算平方根
cbrt(x) 计算立方根
pow(x,y) 计算x的y次方
fabs(x) 计算x的绝对值
ceil(x) 向上取整
floor(x) 向下取整
round(x) 四舍五入取整
trunc(c) 截断小数部分

以上函数返回的都是 double 类型。

2.指针

2.1指针基础

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n994" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">1.如果一个变量专门用来存放内存地址,则称它为指针变量,通常简称为指针。
2.内存地址
在32位系统中,内存地址通常是32位二进制数字,即4个字节
在64位系统中,内存地址通常是64位二进制数字,即8个字节
3.取地址运算符 &
同&可以将某个变量的地址获取
如果需要使用printf输出一个内存地址可以使用占位符%p
4.取值运算符(解引用运算符) *
可以获取指针所指向的数据</pre>

2.2指针运算

①指针加减整数

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n997" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">加n,向后移动n指向类型的字节数
减n,向前移动n
指向类型的字节数
</pre>

②指针自增自减

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n999" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">自增:地址向后移1个元素(一个类型大小)
自减:地址向前移一个元素(一个类型大小)</pre>

③同类型指针相加减

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n1002" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">1.同类型指针相减的结果:相差的元素个数(两个地址相差的内存单元地址个数/指向的类型大小)
2.后面地址减前面的地址得到正值,前面地址减后面地址得到负值
3.同类型指针相减的结果是ptrdiff_t类型,是个有符号整数,对应的占位符是%td</pre>

④指针大小比较

后面地址大于前面地址

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容