今天开始学习函数。
函数是一个大型程序中某部分代码,由一个或者多个语句快组成,负责完成某项特定恩物,相较于其他代码具备独立性。
一般有输入参数并有返回值。
库函数:对于大量频繁使用的函数比如printf,直接调用一个库,然后可以不用编译函数直接使用库中函数。
库函数分类:
IO函数输入输出函数:printf、scanf、getchar、putchar
字符串操作函数:strcmp、strlen
字符操作函数:toupper
内存操作函数:memcpy、memcmp、memset
时间操作函数:time
数学函数:sqrt、pow
其他库函数
接下来学习下如何使用www.cplusplus.com学习函数
首先打开网页
发现跟比特课件上的完全不同也没有search。不要慌,点一下右上角的legacy version切换成老的就可以搜索啦。然后做两个举例下。
Strcpy:
可以通过阅读解释知道这个函数是干什么的。本人留学生这点初中英语还是手拿把掐的。
char * strcpy ( char * destination, const char * source );
Copy string
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.
简单的说就是:把source的字符复制然后放到destination里去。我们来试一下:
int main()
{
char arr1[20] = { 0 };
char arr2[] = "hello bit";
strcpy(arr1, arr2);
printf("%s", arr1);
return 0;
}
运行结果:
Ok。
注意:一定要先引用头文件,这个函数不是自带的,使用的话必须引用一下。如图我在开始就引用了includea<string.h>所以我才可以用,没有引用会报错。
Memset:
我们试一下:
int main()
{
char arr[] = "hello bit";
memset(arr, 'x', 5);
printf("%s", arr);
return 0;
}
把前五个字都换成了x