C语言常用库函数

本文介绍了C语言中几个常用的库函数,包括tolower()和toupper()用于字母大小写的转换,memset()用于填充内存块,以及qsort()实现快速排序。这些函数在不同的编程场景中非常实用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tolower()    把英文字母转换成小写,非字母字符不做出处理   ctype.h中

toupper()   把英文字母转换成大写,非字母字符不做出处理   ctype.h中

memset()  原型:void *memset(void *s, int ch, size_t n)   将s所指向的某一块内存中的前n个 字节(每个字节每个字节的赋)的内容全部设置为ch指定的ASCII值   stdlib.h中                            返回值为指向s的指针    如: int s[100],memset(s,0,sizeof(s));     char s[100];memset(s,'*',sizeof(s));

qsort()   原型:void qsort(void*base,size_t num,size_t width,int(__cdecl*compare)(const void*,const void*))    各参数:1 待排序数组首地址 2 数组中               待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针        使用快速排序历程进行排序   stdlib.h中   如:   

               

对一维数组的排序实例(从小到大排序):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<stdio.h>
#include<stdlib.h>
int  comp( const  void *a, const  void *b)
{
return  *( int *)a-*( int *)b;
}
int  main()
{
int  i=0;
int  *array;
int  n;
scanf ( "%d" ,&n);
array=( int *) malloc (n* sizeof ( int ));
 
for (;i<n;i++)
{
scanf ( "%d" ,(array+i));
}
qsort (array,n, sizeof ( int ),comp);
for (i=0;i<n;i++)
{
printf ( "%d\t" ,array[i]);
}
return  0;
}
对一个 二维数组进行排序:
int a[1000][2]; 其中按照a[0]的大小进行一个整体的排序,其中a[1]必须和a[0]一起移动交换。//即第一行和第二行(a[0]和a[1]分别代表第一行和第二行的首地址)。使用库函数排序的代码量并不比用冒泡排序法小,但速度却快很多。
1
2
3
4
5
6
7
qsort (a,1000, sizeof ( int )*2,comp);
 
int  comp( const  void *a, const  void *b)
 
{
return (( int *)a)[0]-(( int *)b)[0];
}
对字符串进行排序
1
2
3
4
5
6
7
8
9
10
int  Comp( const  void *p1, const  void *p2)
{
return  strcmp (( char *)p2,( char *)p1);
}
int  main()
{
char  a[MAX1][MAX2];
initial(a);
qsort (a,lenth, sizeof (a[0]),Comp);
//lenth为数组a的长度
按结构体中某个关键字排序(对结构体一级排序):
1
2
3
4
5
6
7
8
9
10
structNode
{
double  data;
int  other;
}s[100];
int  Comp(constvoid*p1,constvoid*p2)
{
return (*(Node*)p2).data>(*(Node*)p1).data?1:-1;
}
qsort (s,100, sizeof (s[0]),Comp);
按结构体中多个关键字排序(对结构体多级排序)[以二级为例]:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct  Node
{
int  x;
int  y;
}s[100];
//按照x从小到大排序,当x相等时按y从大到小排序
int  Comp( const  void *p1, const  void *p2)
{
struct  Node*c=(Node*)p1;
struct  Node*d=(Node*)p2;
if (c->x!=d->x)returnc->x-d->x;
else  return  d->y-c->y;
}
对结构体中字符串进行排序:
1
2
3
4
5
6
7
8
9
10
11
struct  Node
{
int  data;
char  str[100];
}s[100];
//按照结构体中字符串str的字典序排序
int  Comp( const  void *p1, const  void *p2)
{
return  strcmp ((*(Node*)p1).str,(*(Node*)p2).str);
}
qsort (s,100, sizeof (s[0]),Comp);
计算几何中求凸包的Comp
1
2
3
4
5
6
7
8
9
10
11
int  Comp( const  void *p1, const  void *p2)
//重点Comp函数,把除了1点外的所有的点旋转角度排序
{
struct  point*c=(point*)p1;
struct  point*d=(point*)p2;
if (cacl(*c,*d,p[1])<0) return1;
elseif(!cacl(*c,*d,p[1])&&dis(c->x,c->y,p[1].x,p[1].y)<dis(d->x,d->y,p[1].x,p[1].y))
//如果在一条直线上,则把远的放在前面
return1;
elsereturn-1;
}
sprintf()    把格式化的数据写入某个字符串中  stdio.h中  原型:int sprintf( char *buffer, const char *format, [ argument…);                  bufferchar型指针,指向将要写入的字符串的缓冲区。 format:格式化字符串。 [argument]...:可选参数,可以                  是任何类型的数据。        返回值:字符串长度,结束字符‘\0’不计入内, 出错返回-1   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// crt_sprintf.c
// compile with: /W3
// This program uses sprintf to format various
 
//data and place them in the string named buffer.
// 程序使用sprintf 将各种数据格式化后置于字符数组buffer中
#include <stdio.h>
int  main(  void  )
 
{
    char   buffer[200], s[] =  "computer" , c =  'l' ;
    int    i = 35, j;
    float  fp = 1.7320534f;
    // 格式化并打印各种数据到buffer
    j  =  sprintf ( buffer,     "   String:    %s\n" , s );  // C4996
    j +=  sprintf ( buffer + j,  "   Character: %c\n" , c );  // C4996
    j +=  sprintf ( buffer + j,  "   Integer:   %d\n" , i );  // C4996
    j +=  sprintf ( buffer + j,  "   Real:      %f\n" , fp ); // C4996
    
    printf "Output:\n%s\ncharacter count = %d\n" , buffer, j );
}
输出结果 [1]   :
Output:
String: computer 
  
Character: l
Integer: 35
Real: 1.732053
character count = 79
strchr()   原型: char *strchr(const char* _Str,int _Val)  或  char *strchr(char* _Str,int _Ch)   查找 字符 串s中首次出现字符c                  的位置。   string.h中  返回首次出现c的位置的指针,返回的地址是被查找字符串指针开始的第一个与Val相同字符的指                针,如果s中不存在c则返回NULL
 strstr()   strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。  string.h中
isprintf()  判断字符c是否为可打印字符(含空格)说明:当c为可打印字符(0x20-0x7e)时,返回非零值,否则返回零。 ctype.h中  
clock()  返回值为长整型,在time.h中,得到程序运行到此的运行时间,clock()/CLOCK_PER_SEC得到以秒为计量的时间。(CLOCK_PER_SEC表示一秒钟有多少个时钟计时单位
isdigit() 检查参数c是否为字符阿拉伯数字0到9 ,等价于if(c>='0'&&c<='9')   若参数c为阿拉伯数字,则返回非0值(不一定是1或TRUE,因为TRUE值和具体编译器相关),否则返回NULL(0)   ctype.h中
strpbrk()   在源字符串(s1)中找出最先含有搜索字符串(s2)中任一字符的位置并返回,若找不到则返回空指针。 string.h中
memcpy()  原型: void *memcpy(void *dest, const void *src, size_t n)   从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。 string.h中  如:int a[10],b[10];memcpy(b,a,sizeof(int)*k); double a[10],b[10];memcpy(b,a,sizeof(double)*k);
isalpha()   判断字符ch是否为英文字母,若为英文字母,返回1。若不是字母,返回0。在标准c中相当于使用“ isupper( ch ) || islower( ch ) ”做测试  ctype.h中
isupper()  判断 字符 c是否为大写英文字母, 说明:当参数c为大写英文字母(A-Z)时,返回非零值,否则返回零。ctype.h中
islower()   判断 字符 c是否为小写英文字母, 说明:当参数c为小写英文字母(A-Z)时,返回TRUE,否则返回零(NULL)。ctype.h中
.  atof():
功 能: 把字符串转换成浮点数
名字来源:ascii to floating point numbers 的缩写
用 法: double atof(const char *nptr);
中文名
atof()
外文名
ascii to floating point numbers
释    义
. 函数名
功 能
 把字符串转换成浮点数

程序举例

编辑
程序例:
1
2
3
4
5
6
7
8
9
10
#include<stdlib.h>
#include<stdio.h>
int  main()
{
double  d;
char  str[] =  "123.456" ;
d= atof (str);
printf ( "string=%sdouble=%lf\n" ,str,d);
return  0;
}

基本介绍

编辑
2. atof(将字串转换成 浮点型数)
相关函数  atoiatolstrtodstrtolstrtoul
表头文件 #include <stdlib.h>
定义函数 double atof(const char *nptr);
函数说明 atof()会扫描参数nptr 字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。
返回值 返回转换后的 浮点型数。
附加说明 atof()与使用 strtod(nptr,(char**)NULL)结果相同。
范例 /* 将字符串a 与字符串b转换成数字后相加*/
1
2
3
4
5
6
7
8
9
10
#include<stdlib.h>
int  main()
{
char *a= "-100.23" ;
char *b= "200e-2" ;
doublec;
c= atof (a)+ atof (b);
printf (“c=%.2lf\n”,c);
return  0;
}
执行 c=-98.23
hypot():
功 能: 计算直角三角形的斜边长。
头文件:<math.h>、<cmath> 。
用 法: double hypot(double x, double y);
floor(): 其功能是“向下取整”,或者说“向下舍入”,即取不大于x的最大整数,返回值为double math.h中
ceil():其功能是“向上取整”,即取不小于x的最小整数,返回值为double  math.h中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值