C语言学习笔记——函数(三)

简介: C语言学习笔记——函数

11.字符串中常用的系统函数

字符串的头文件<string.h>

1.得到字符串的长度

size_t strlen(const char *str)//计算字符串 str 的长度,直到空结束字符,但不包括空结束字符

2.拷贝字符串

char *strcpy(char *dest, const char *src)//把 src 所指向的字符串复制到 dest

3.连接字符串

char *strcat(char *dest, const char *src)//把 src 所指向的字符串追加到 dest 所指向的字符串的结尾。

4.代码示例

#include<stdio.h>
#include<string.h>
void main() {
  char* test01 = "江南";//使用size_t strlen(const char *str)得到字符串的长度
  char test02[50] ="", test03[50] = "";
  printf("test01的长度为%d\n", strlen(test01));
  //使用char *strcpy(char *dest, const char *src)拷贝字符串
  //使用strcpy时,数组原来的内容将会被覆盖
  strcpy_s(test02, strlen(test01) + 1, test01);
  printf("test02 = %s\n", test02);
  //连接字符串char *strcat(char *dest, const char *src)
  strcpy_s(test03, strlen(test01) + 1, test01);//test03里的内容为“江南”
  strcat_s(test03, strlen(test01) + strlen(test03) +1, test01);
  printf("test03 = %s\n", test03);
  //printf("test03 = %s\n", test03);
}

51ddff40a2104d2ab558bd21e32a7096.png

12.时间和日期相关函数

时间的头文件为<time.h>

1.获取当前时间

char *ctime(const time_t *timer)//返回一个表示当地时间的字符串,当地时间是基于参数 timer
#include<stdio.h>
#include<time.h>
void main() {
  time_t curtime;//time_t是一个结构体类型,定义一个名称叫curtime的time_t结构体
  time(&curtime);//time()初始化
  char time[50];
  ctime_s(time, sizeof(time), &curtime);
  printf("当前时间 = %s", time);
}

3ffe3505e0394a578f7fb5d25f976493.png

2.编写一段代码来统计 函数 test 执行的时间

double difftime(time_t time1, time_t time2)//返回 time1 和 time2 之间相差的秒数 (time1-time2)
#include<stdio.h>
#include<time.h>
void test() {
  int a, b, sum=0;
  for (a = 0; a < 7777777; a++) {
    for (b = 0; b < 300; b++) {
      sum += b;
    }
  }
}
void main() {
  time_t startTime, endTime;
  double differentTime = 0.0;
  printf("程序启动!");
  time(&startTime);
  test();
  time(&endTime);
  differentTime = difftime(endTime, startTime);
  printf("程序运行时间为%.2f", differentTime);
}

d2fc4dbe870b48da82d7260e4340ba74.png

13.数学相关函数

1.

double exp(double x)//返回 e 的 x 次幂的值
double log(double x)//返回 x 的自然对数(基数为 e 的对数)
double pow(double x, double y)//返回 x 的 y 次幂
double sqrt(double x)//返回 x 的平方根
double fabs(double x)//返回 x 的绝对值

2.代码示例

#include<stdio.h>
#include<math.h>
void main() {
  double result1 = exp(7.0);
  printf("e的7次方 = %f\n", result1);
  double result2 = log(7.0);
  printf("ln7 = %f\n", result2);
  double result3 = pow(9.0, 7.0);
  printf("9的7次方 = %f\n", result3);
  double result4 = sqrt(7.0);
  printf("根号7 = %f\n", result4);
  double result5 = fabs(-7.0);
  printf("-7的绝对值 = %f", result5);
}

87eef25c002b44688bd1851f26be2f1b.png

14.基本数据类型和字符串类型的转换

14.1.基本介绍

在程序开发中,我们经常需要将基本数据类型转成字符串类型(即 char 数组 ),或者将字符串类型转成基本数据类型

14.2.sprintf函数的用法

sprintf和平时我们常用的 printf函数的功能很相似。sprintf函数打印到字符串中,而 printf函数打印输出到屏幕 上。sprintf函数在我们完成其他数据类型转换成字符串类型的操作中应用广泛。该函数包含在 stdio.h 的头文件中

14.3.基本类型转字符串类型

#include<stdio.h>
void main() {
  char str1[20], str2[20], str3[20];
  int num1 = 97, num3 = 977;
  double num2 = 97.777777;
    //将num1和num3的内容存入str1中,其中按照num1→num3的顺序
  sprintf_s(str1, sizeof(str1), "%d%d", num1, num3);
    //将num2的内容保留两个小数点存入str2
  sprintf_s(str2, sizeof(str2), "%.2f", num2);
    //将num2的内容一共保留七位数字,其中两位小数,保存到str3中,不足的位数用空格补齐
  sprintf_s(str3, sizeof(str3),"%7.2f", num2);
  printf("str1 = %s\nstr2 = %s\nstr3 = %s", str1, str2, str3);
}

93711c9fda4e4b02be6e3c43b2d458ce.png

14.4符串类型转基本数据类型

通过<stdlib.h>的函数调用 atoi atof 即可

#include<stdio.h>
#include<stdlib.h>
void main() {
  char str1[77] = "12.34567";
  char str2[77] = "123.4567";
  char str3[77] = "abcdefg";
  char str4[77] = "1234567";
  int num1 = atoi(str1);//atoi(str1)将str1转换为整数
  int num2 = str4;
  double num3 = atof(str2);//atof(str2)将str2转换为浮点数
  char num4 = str3[6];//表示取出str3这个字符串的第七个字符'g'
  //因为str3[6]是从str[0]开始计算 0 1 2 3 4 5 6 第七个
  printf("num1 = %d\nnum 2 = %d\nnum 3 = %f\nnum 4 = % c", num1, num2, num3, num4);
}

dbb669255a0f46509333a5549f3f3a79.png

14.5.注意事项

  1. 在将 char 数组 类型转成 基本数据类型时,要确保能够转成有效的数据。例如,可以把"777"转为数字,但是不能将"jiangnan"转为一个整数
  2. 如果格式不正确,会默认转成 0 或者 0.0


相关文章
|
10天前
|
C语言 C++
C语言 之 内存函数
C语言 之 内存函数
25 3
|
1天前
|
存储 缓存 C语言
【c语言】简单的算术操作符、输入输出函数
本文介绍了C语言中的算术操作符、赋值操作符、单目操作符以及输入输出函数 `printf` 和 `scanf` 的基本用法。算术操作符包括加、减、乘、除和求余,其中除法和求余运算有特殊规则。赋值操作符用于给变量赋值,并支持复合赋值。单目操作符包括自增自减、正负号和强制类型转换。输入输出函数 `printf` 和 `scanf` 用于格式化输入和输出,支持多种占位符和格式控制。通过示例代码详细解释了这些操作符和函数的使用方法。
17 10
|
5天前
|
存储 编译器 C语言
C语言函数的定义与函数的声明的区别
C语言中,函数的定义包含函数的实现,即具体执行的代码块;而函数的声明仅描述函数的名称、返回类型和参数列表,用于告知编译器函数的存在,但不包含实现细节。声明通常放在头文件中,定义则在源文件中。
|
11天前
|
C语言
c语言回顾-函数递归(上)
c语言回顾-函数递归(上)
27 2
|
13天前
|
Java 编译器 C语言
【一步一步了解Java系列】:Java中的方法对标C语言中的函数
【一步一步了解Java系列】:Java中的方法对标C语言中的函数
16 3
|
13天前
|
C语言
C语言学习笔记-知识点总结上
C语言学习笔记-知识点总结上
56 1
|
10天前
|
C语言
C语言函数
C语言函数
10 0
|
11天前
|
C语言 C++
c语言回顾-内存操作函数
c语言回顾-内存操作函数
34 0
|
11天前
|
C语言
c语言回顾-函数递归(下)
c语言回顾-函数递归(下)
29 0
|
13天前
|
Serverless C语言
C语言函数基础
C语言函数基础
20 0