常用函数的用法
一、概述
函数是计算机编程中的重要概念,它可以将一些操作封装起来,方便代码的复用和管理。在编写程序时,我们经常会使用一些常用函数来完成特定的任务。本文将介绍一些常用函数的用法。
二、字符串函数
1. strlen()
strlen()函数可以返回一个字符串的长度,即其中字符的个数。例如:
```
char str[] = "hello world";
int len = strlen(str);
ftell函数用法
printf("字符串长度为%d\n", len);
```
输出结果为:
```
字符串长度为11
```
2. strcpy()
strcpy()函数可以将一个字符串复制到另一个字符串中。例如:
```
char src[] = "hello world";
char dest[20];
strcpy(dest, src);
printf("复制后的字符串为%s\n", dest);
```
输出结果为:
```
复制后的字符串为hello world
```
3. strcat()
strcat()函数可以将一个字符串追加到另一个字符串末尾。例如:
```
char str1[] = "hello";
char str2[] = "world";
strcat(str1, str2);
printf("拼接后的字符串为%s\n", str1);
```
输出结果为:
```
拼接后的字符串为helloworld
```
4. strcmp()
strcmp()函数可以比较两个字符串是否相等。如果相等,则返回0;如果第一个参数小于第二个参数,则返回负数;否则返回正数。例如:
```
char str1[] = "hello";
char str2[] = "world";
int result = strcmp(str1, str2);
if (result == 0) {
    printf("两个字符串相等\n");
} else if (result < 0) {