C语言,常用的系统函数

字符串中常用的系统函数

说明:字符串(即字符数组)在我们程序开发中,使用的是非常多的,常用的函数需要同学们掌握 [带看手册或 者官方编程指南 头文件 <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)字符串对比 int strcmp(const char *str1, const char *str2) 把 str1 所指向的字符串和 str2 所指向的字符串进行比较。

#include<stdio.h>
#include<string.h>

void main(){
	
	char name[50] = "张无忌";
	printf("%s",name); 
	
	char* pwd = "123456"; //指针类型 
	printf("\n%s",pwd);
	
	//strlen : 查看字符串大小 
	printf("\n大小=%d",strlen(name));
	
	char password[10] = "888";
	
	//将 pwd的内容拷贝到password内容中。 
	strcpy(name,pwd);
	printf("\nname = %s",name);
	
	//strcat 是将 src 字符串的内容连接到 dest ,但是不会覆盖 dest 原来的内容,而是连接!! 
	strcat(password, pwd); // " 888123456 " 
	printf("\n最终的目标字符串: pasword=%s", password);
	
}

时间和日期相关函数

说明:在编程中,程序员会经常使用到日期相关的函数,比如:统计某段代码执行花费的时间等等。 头文件是 <time.h> 举例说明:

1) 获取当前时间 char *ctime(const time_t *timer) 返回一个表示当地时间的字符串,当地时间是基于参数 timer。
2)编写一段代码来统计 函数 test 执行的时间 double difftime(time_t time1, time_t time2) 返回 time1 和 time2 之间相差的秒数 (time1-time2)。
3) 代码演示

#include<stdio.h>
#include<time.h>

void main(){
	
	time_t curtime; //time_h 是一个结构体类型 
	time(&curtime); //time() 
	//ctime 返回一个表示当地时间的字符串,当地时间是基于参数 timer 
	printf("当前时间 = %s", ctime(&curtime)); 
	
}

在这里插入图片描述

#include<stdio.h>
#include<time.h> //该头文件中,声明和日期和时间相关的函数

void test() { // 运行 test 函数,看看执行花费时间 
	int i = 0; 
	int sum = 0; 
	int j = 0; 
	for(i = 0; i < 777777777;i++) { 
		sum = 0; 
		for (j = 0; j< 10;j++) { 
			sum += j; 
		} 
	} 
}

void main(){
	
	//先得到执行 test 前的时间 
	time_t start_t, end_t; 
	double diff_t; //存放时间差 
	printf("程序启动...\n"); 
	time(&start_t); //初始化得到当前时间 test(); 
	
	//执行 test 
	//再得到执行 test 后的时间 
	time(&end_t); //得到当前时间 
	
	
	diff_t = difftime(end_t, start_t); //时间差,按秒 ent_t - start_t 
	//然后得到两个时间差就是耗用的时间 
	printf("执行 test()函数 耗用了%.2f 秒", diff_t);
	
}

数学相关函数

math.h 头文件定义了各种数学函数和一个宏。在这个库中所有可用的功能都带有一个 double 类型的参数,且都返 回 double 类型的结果 举例说明:
1)double exp(double x) 返回 e 的 x 次幂的值。
2) double log(double x) 返回 x 的自然对数(基数为 e 的对数)
3)double pow(double x, double y) 返回 x 的 y 次幂。
4)double sqrt(double x) 返回 x 的平方根。
5)double fabs(double x) 返回 x 的绝对值。
6) 代码演示

#include <stdio.h> 
#include <math.h> 

void main (){ 
	double d1 = pow(2.0,3.0);
 	double d2 = sqrt(5.0); 
 	printf("d1=%.2f", d1); 
 	printf("d2=%.2f", d2); 
}

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

介绍

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

sprintf 函数的用法

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

基本类型转字符串类型

案例演示:

#include <stdio.h> 

void main() {

	char str1[20]; //字符数组,即字符串 
	char str2[20]; 
	char str3[20]; 
	int a=20984,b=48090; 
	double d=14.309948; 
	//说明 
	//1. sprintf 是一个系统函数,可以将结果存放到字符串中 
	//2. 格式化的结果,会存放到 str1 中 
	sprintf(str1,"%d %d",a,b); 
	sprintf(str2, "%.2f", d); 
	sprintf(str3, "%8.2f", d); //%8.2f 含义是格式化后,一共有 8 位,小数点后占用 2 位, 不够用空格占位 
	printf("str1=%s str2=%s str3=%s", str1, str2, str3);
}

在这里插入图片描述

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

语法:通过<stdlib.h>的函数调用 atoi atof 即可 ,案例演示

#include <stdio.h> 
#include <stdlib.h> 

void main() { 
	//字符数组 
	char str[10] = "123456"; 
	char str2[10] = "12.67423";
	char str3[3] = "ab"; 
	char str4[4] = "111"; 
	
	//说明 
	//1. atoi(str) 将 str 转成整数 
	int num1 = atoi(str); 
	short s1 = atoi(str4); 
	
	//说明 
	//1. atof(str2); 将 str2 转成小数 
	double d = atof(str2); 
	
	//说明 
	//1. str3[0] 表示获取到 str3 这个字符串(数组)的第一个元素 'a' 
	char c = str3[0]; 
	printf("num1=%d \t d=%f \t c=%c \t s1=%d", num1, d, c, s1);
}

在这里插入图片描述

注意事项

1)在将 char 数组 类型转成 基本数据类型时,要确保能够转成有效的数据,比如 我们可以把 “123” , 转成一个 整数,但是不能把 “hello” 转成一个整数
2)如果格式不正确,会默认转成 0 或者 0.0

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值