linux 常用C函数系列之二

来源:互联网 发布:网络教育本科出国留学 编辑:程序博客网 时间:2024/05/01 12:18

3. 内存控制篇
3.1 calloc(配置内存空间) 
相关函数  malloc,free,realloc,brk
表头文件  #include <stdlib.h>
定义函数  void *calloc(size_t nmemb,size_t size);
函数说明  calloc()用来配置nmemb个相邻的内存单位,每一单位的大小为size,并返回指向第一个元素

的指针。这和使用下列的方式效果相同:malloc(nmemb*size);不过,在利用calloc()配置内存时会将内存

内容初始化为0。
返回值  若配置成功则返回一指针,失败则返回NULL。
范例  /* 动态配置10个struct test 空间*/
#include<stdlib.h>
struct test
{
int a[10];
char b[20];
}
main()
{
struct test *ptr=calloc(sizeof(struct test),10);
}
 
  
3.2 free(释放原先配置的内存) 
相关函数  malloc,calloc,realloc,brk
表头文件  #include<stdlib.h>
定义函数  void free(void *ptr);
函数说明  参数ptr为指向先前由malloc()、calloc()或realloc()所返回的内存指针。调用free()后ptr所指的

内存空间便会被收回。假若参数ptr所指的内存空间已被收回或是未知的内存地址,则调用free()可能会

有无法预期的情况发生。若参数ptr为NULL,则free()不会有任何作用。
 

3.3 getpagesize(取得内存分页大小) 
相关函数  sbrk
表头文件  #include<unistd.h>
定义函数  size_t getpagesize(void);
函数说明  返回一分页的大小,单位为字节(byte)。此为系统的分页大小,不一定会和硬件分页大小

相同。
返回值  内存分页大小。附加说明在Intel x86 上其返回值应为4096bytes。
范例  #include <unistd.h>
main()
{
printf(“page size = %d/n”,getpagesize( ) );
}
 
 
3.4 malloc(配置内存空间) 
相关函数  calloc,free,realloc,brk
表头文件  #include<stdlib.h>
定义函数  void * malloc(size_t size);
函数说明  malloc()用来配置内存空间,其大小由指定的size决定。
返回值  若配置成功则返回一指针,失败则返回NULL。
范例  void p = malloc(1024); /*配置1k的内存*/
 

3.5 mmap(建立内存映射) 
相关函数  munmap,open
表头文件  #include <unistd.h>
#include <sys/mman.h>
定义函数  void *mmap(void *start,size_t length,int prot,int flags,int fd,off_t offsize);
函数说明  mmap()用来将某个文件内容映射到内存中,对该内存区域的存取即是直接对该文件内容的读

写。参数start指向欲对应的内存起始地址,通常设为NULL,代表让系统自动选定地址,对应成功后该

地址会返回。参数length代表将文件中多大的部分对应到内存。
参数  prot代表映射区域的保护方式有下列组合
PROT_EXEC 映射区域可被执行
PROT_READ 映射区域可被读取
PROT_WRITE 映射区域可被写入
PROT_NONE 映射区域不能存取
 
参数  flags会影响映射区域的各种特性
MAP_FIXED 如果参数start所指的地址无法成功建立映射时,则放弃映射,不对地址做修正。通常不鼓

励用此旗标。
MAP_SHARED对映射区域的写入数据会复制回文件内,而且允许其他映射该文件的进程共享。
MAP_PRIVATE 对映射区域的写入操作会产生一个映射文件的复制,即私人的“写入时复制”(copy on

write)对此区域作的任何修改都不会写回原来的文件内容。
MAP_ANONYMOUS建立匿名映射。此时会忽略参数fd,不涉及文件,而且映射区域无法和其他进程共

享。
MAP_DENYWRITE只允许对映射区域的写入操作,其他对文件直接写入的操作将会被拒绝。
MAP_LOCKED 将映射区域锁定住,这表示该区域不会被置换(swap)。
在调用mmap()时必须要指定MAP_SHARED 或MAP_PRIVATE。参数fd为open()返回的文件描述词,

代表欲映射到内存的文件。参数offset为文件映射的偏移量,通常设置为0,代表从文件最前方开始对应

,offset必须是分页大小的整数倍。
 
返回值  若映射成功则返回映射区的内存起始地址,否则返回MAP_FAILED(-1),错误原因存于errno

中。
 
错误代码  EBADF 参数fd 不是有效的文件描述词
EACCES 存取权限有误。如果是MAP_PRIVATE 情况下文件必须可读,使用MAP_SHARED则要有

PROT_WRITE以及该文件要能写入。
EINVAL 参数start、length 或offset有一个不合法。
EAGAIN 文件被锁住,或是有太多内存被锁住。
ENOMEM 内存不足。
 
范例  /* 利用mmap()来读取/etc/passwd 文件内容*/
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/mman.h>
main()
{
int fd;
void *start;
struct stat sb;
fd=open(“/etc/passwd”,O_RDONLY); /*打开/etc/passwd*/
fstat(fd,&sb); /*取得文件大小*/
start=mmap(NULL,sb.st_size,PROT_READ,MAP_PRIVATE,fd,0);
if(start= = MAP_FAILED) /*判断是否映射成功*/
return;
printf(“%s”,start);
munma(start,sb.st_size); /*解除映射*/
closed(fd);
}
 
执行  root : x : 0 : root : /root : /bin/bash
bin : x : 1 : 1 : bin : /bin :
daemon : x : 2 : 2 :daemon : /sbin
adm : x : 3 : 4 : adm : /var/adm :
lp : x :4 :7 : lp : /var/spool/lpd :
sync : x : 5 : 0 : sync : /sbin : bin/sync :
shutdown : x : 6 : 0 : shutdown : /sbin : /sbin/shutdown
halt : x : 7 : 0 : halt : /sbin : /sbin/halt
mail : x : 8 : 12 : mail : /var/spool/mail :
news : x :9 :13 : news : /var/spool/news :
uucp : x :10 :14 : uucp : /var/spool/uucp :
operator : x : 11 : 0 :operator : /root:
games : x : 12 :100 : games :/usr/games:
gopher : x : 13 : 30 : gopher : /usr/lib/gopher-data:
ftp : x : 14 : 50 : FTP User : /home/ftp:
nobody : x :99: 99: Nobody : /:
xfs :x :100 :101 : X Font Server : /etc/xll/fs : /bin/false
gdm : x : 42 :42 : : /home/gdm: /bin/bash
kids : x : 500 :500 :/home/kids : /bin/bash
 
 
3.6 munmap(解除内存映射) 
相关函数  mmap
表头文件  #include<unistd.h>
#include<sys/mman.h>
定义函数  int munmap(void *start,size_t length);
函数说明  munmap()用来取消参数start所指的映射内存起始地址,参数length则是欲取消的内存大小。

当进程结束或利用exec相关函数来执行其他程序时,映射内存会自动解除,但关闭对应的文件描述词

时不会解除映射。
返回值  如果解除映射成功则返回0,否则返回-1,错误原因存于errno中错误代码EINVAL
参数  start或length 不合法。
范例  参考mmap()
 


4. 日期时间篇
4.1 asctime(将时间和日期以字符串格式表示) 
相关函数  time,ctime,gmtime,localtime
表头文件  #include<time.h>
定义函数  char * asctime(const struct tm * timeptr);
函数说明  asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,

然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:“Wed Jun 30

21:49:08 1993/n”
返回值  若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数

是不同的结构。
附加说明  返回一字符串表示目前当地的时间日期。
范例  #include <time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,asctime(gmtime(&timep)));
}
 
执行  Sat Oct 28 02:10:06 2000

 
4.2 ctime(将时间和日期以字符串格式表示) 
相关函数  time,asctime,gmtime,localtime
表头文件  #include<time.h>
定义函数  char *ctime(const time_t *timep);
函数说明  ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,

然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为“Wed Jun 30 21 :49

:08 1993/n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
返回值  返回一字符串表示目前当地的时间日期。
范例  #include<time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,ctime(&timep));
}
 
执行  Sat Oct 28 10 : 12 : 05 2000
 
 
4.3 gettimeofday(取得目前的时间) 
相关函数  time,ctime,ftime,settimeofday
表头文件  #include <sys/time.h>
#include <unistd.h>
定义函数  int gettimeofday ( struct timeval * tv , struct timezone * tz )
函数说明  gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中


timeval结构定义为:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 结构定义为:
struct timezone{
int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下
DST_NONE /*不使用*/
DST_USA /*美国*/
DST_AUST /*澳洲*/
DST_WET /*西欧*/
DST_MET /*中欧*/
DST_EET /*东欧*/
DST_CAN /*加拿大*/
DST_GB /*大不列颠*/
DST_RUM /*罗马尼亚*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/
 
返回值  成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空

间超出存取权限。
 
范例  #include<sys/time.h>
#include<unistd.h>
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf(“tv_sec; %d/n”, tv,.tv_sec) ;
printf(“tv_usec; %d/n”,tv.tv_usec);
printf(“tz_minuteswest; %d/n”, tz.tz_minuteswest);
printf(“tz_dsttime, %d/n”,tz.tz_dsttime);
}
 
执行  tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
 
 
4.4 gmtime(取得目前时间和日期) 
相关函数  time,asctime,ctime,localtime
表头文件  #include<time.h>
定义函数  struct tm*gmtime(const time_t*timep);
函数说明  gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方

法,然后将结果由结构tm返回。
结构tm的定义为
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min 代表目前分数,范围0-59
int tm_hour 从午夜算起的时数,范围为0-23
int tm_mday 目前月份的日数,范围01-31
int tm_mon 代表目前月份,从一月算起,范围从0-11
int tm_year 从1900 年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
 
返回值  返回结构tm代表目前UTC 时间
 
范例  #include <time.h>
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf(“%s%d;%d;%d/n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
 
执行  2000/10/28 Sat 8:15:38
 

4.5 localtime(取得当地目前时间和日期) 
相关函数  time, asctime, ctime, gmtime
表头文件  #include<time.h>
定义函数  struct tm *localtime(const time_t * timep);
函数说明  localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方

法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当

地时区。
返回值  返回结构tm代表目前的当地时间。
范例  #include<time.h>
main(){
char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); /*取得当地时间*/
printf (“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf(“%s%d:%d:%d/n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
 
执行  2000/10/28 Sat 11:12:22
 

4.6 mktime(将时间结构数据转换成经过的秒数) 
相关函数  time,asctime,gmtime,localtime
表头文件  #include<time.h>
定义函数  time_t mktime(strcut tm * timeptr);
函数说明  mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至

今的UTC时间所经过的秒数。
返回值  返回经过的秒数
范例  /* 用time()取得时间(秒数),利用localtime()
转换成struct tm 再利用mktine()将struct tm转换成原来的秒数*/
#include<time.h>
main()
{
time_t timep;
strcut tm *p;
time(&timep);
printf(“time() : %d /n”,timep);
p=localtime(&timep);
timep = mktime(p);
printf(“time()->localtime()->mktime():%d/n”,timep);
}
 
执行  time():974943297
time()->localtime()->mktime():974943297
 
 
4.7 settimeofday(设置目前时间) 
相关函数  time,ctime,ftime,gettimeofday
表头文件  #include<sys/time.h>
#include<unistd.h>
定义函数  int settimeofday ( const struct timeval *tv,const struct timezone *tz);
函数说明  settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。

详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。
返回值  成功则返回0,失败返回-1,错误代码存于errno。
错误代码  EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。
 
 
4.8 time(取得目前的时间) 
相关函数  ctime,ftime,gettimeofday
表头文件  #include<time.h>
定义函数  time_t time(time_t *t);
函数说明  此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t

并非空指针的话,此函数也会将返回值存到t指针所指的内存。
返回值  成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
范例  #include<time.h>
mian()
{
int seconds= time((time_t*)NULL);
printf(“%d/n”,seconds);
}
 
执行  9.73E+08
 


5. 内存及字符串操作篇
5.1 bcmp(比较内存内容) 
相关函数  bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp
表头文件  #include<string.h>
定义函数  int bcmp ( const void *s1,const void * s2,int n);
函数说明  bcmp()用来比较s1和s2所指的内存区间前n个字节,若参数n为0,则返回0。
返回值  若参数s1 和s2 所指的内存内容都完全相同则返回0 值,否则返回非零值。
附加说明  建议使用memcmp()取代。
范例  参考memcmp()。

 
5.2 bcopy(拷贝内存内容) 
相关函数  memccpy,memcpy,memmove,strcpy,ctrncpy
表头文件  #include <string.h>
定义函数  void bcopy ( const void *src,void *dest ,int n);
函数说明  bcopy()与memcpy()一样都是用来拷贝src所指的内存内容前n个字节到dest所指的地址,不过

参数src与dest在传给函数时是相反的位置。
返回值 
附加说明  建议使用memcpy()取代
范例  #include<string.h>
main()
{
char dest[30]=”string(a)”;
char src[30]=”string/0string”;
int i;
bcopy(src,dest,30);/* src指针放在前*/
printf(bcopy(): “)
for(i=0;i<30;i++)
printf(“%c”,dest[i]);
memcpy(dest src,30); /*dest指针放在钱*/
printf(‘/nmemcpy() : “);
for(i=0;i<30;i++)
printf(“%c”,dest[i]);
 
执行  bcopy() : string string
memcpy() :string sring
 
  
5.3 bzero(将一段内存内容全清为零) 
相关函数  memset,swab
表头文件  #include<string.h>
定义函数  void bzero(void *s,int n);
函数说明  bzero()会将参数s所指的内存区域前n个字节,全部设为零值。相当于调用memset((void*)

s,0,size_tn);
返回值 
附加说明  建议使用memset取代
范例  参考memset()。
 
 
5.4 index(查找字符串中第一个出现的指定字符) 
相关函数  rindex,srechr,strrchr
表头文件  #include<string.h>
定义函数  char * index( const char *s, int c);
函数说明  index()用来找出参数s字符串中第一个出现的参数c地址,然后将该字符出现的地址返回。字

符串结束字符(NULL)也视为字符串一部分。
返回值  如果找到指定的字符则返回该字符所在地址,否则返回0。
范例  #include<string.h>
main()
{
char *s =”0123456789012345678901234567890”;
char *p;
p =index(s,’5’);
printf(%s/n”,p);
}
 
执行  5.68E+25
 
 
5.5  memccpy(拷贝内存内容) 
相关函数  bcopy,memcpy,memmove,strcpy,strncpy
表头文件  #include<string.h>
定义函数  void * memccpy(void *dest, const void * src, int c,size_t n);
函数说明  memccpy()用来拷贝src所指的内存内容前n个字节到dest所指的地址上。与memcpy()不同的

是,memccpy()会在复制时检查参数c是否出现,若是则返回dest中值为c的下一个字节地址。
返回值  返回指向dest中值为c的下一个字节指针。返回值为0表示在src所指内存前n个字节中没有值为c

的字节。
范例  #include<string.h>
main()
{
char a[]="string[a]";
char b[]="string(b)";
memccpy(a,b,'B',sizeof(b));
printf("memccpy():%s/n",a);
}
 
执行  memccpy():string(b)
 
 
5.6 memchr(在某一内存范围中查找一特定字符) 
相关函数  index,rindex,strchr,strpbrk,strrchr,strsep,strspn,strstr
表头文件  #include<string.h>
定义函数  void * memchr(const void *s,int c,size_t n);
函数说明  memchr()从头开始搜寻s所指的内存内容前n个字节,直到发现第一个值为c的字节,则返回

指向该字节的指针。
返回值  如果找到指定的字节则返回该字节的指针,否则返回0
范例  #include <string.h>
main()
{
char *s="0123456789012345678901234567890";
char *p;
p=memchr(s,'5',10);
printf("%s/n",p);
}
 
执行  5.68E+25
 
 
5.7 memcmp(比较内存内容) 
相关函数  bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp
表头文件  #include<string.h>
定义函数  int memcmp (const void *s1,const void *s2,size_t n);
函数说明  memcmp()用来比较s1和s2所指的内存区间前n个字符。字符串大小的比较是以ASCII码表上

的顺序来决定,次顺序亦为字符的值。memcmp()首先将s1第一个字符值减去s2第一个字符的值,若差

为0则再继续比较下个字符,若差值不为0则将差值返回。例如,字符串"Ac"和"ba"比较则会返回字

符'A'(65)和'b'(98)的差值(-33)。
返回值  若参数s1和s2所指的内存内容都完全相同则返回0值。s1若大于s2则返回大于0的值。s1若小于

s2则返回小于0的值。
范例  #include<string.h>
main()
{
char *a ="aBcDeF";
char *b="AbCdEf";
char *c="aacdef";
char *d="aBcDeF";
printf("memcmp(a,b):%d/n",memcmp((void*)a,(void*) b,6));
printf("memcmp(a,c):%d/n",memcmp((void*)a,(void*) c,6));
printf("memcmp(a,d):%d/n",memcmp((void*)a,(void*) d,6));
 
执行  memcmp(a,b):1 /*字符串a>字符串b,返回1*/
memcmp(a,c):-1 /* 字符串a<字符串c,返回-1*/
memcmp(a,d):0 /*字符串a=字符串d,返回0*/
 
 
5.8 memcpy(拷贝内存内容) 
相关函数  bcopy,memccpy,memcpy,memmove,strcpy,strncpy
表头文件  #include<string.h>
定义函数  void * memcpy (void * dest ,const void *src, size_t n);
函数说明  memcpy()用来拷贝src所指的内存内容前n个字节到dest所指的内存地址上。与strcpy()不同的

是,memcpy()会完整的复制n个字节,不会因为遇到字符串结束'/0'而结束。
返回值  返回指向dest的指针。
附加说明  指针src和dest所指的内存区域不可重叠。
范例  #include<string.h>
main()
{
char a[30]="string (a)";
char b[30]="string/0string";
int i;
strcpy(a,b);
printf("strcpy():");
for(i=0;i<30;i++)
printf("%c",a[i]);
memcpy(a,b,30);
printf("/nmemcpy() :");
for(i=0;i<30;i++)
printf("%c",a[i]);
}
 
执行  strcpy() : string a )
memcpy() : string string
 
 
5.9 memmove(拷贝内存内容) 
相关函数  bcopy,memccpy,memcpy,strcpy,strncpy
表头文件  #include<string.h>
定义函数  void * memmove(void *dest,const void *src,size_t n);
函数说明  memmove()与memcpy()一样都是用来拷贝src所指的内存内容前n个字节到dest所指的地址上

。不同的是,当src和dest所指的内存区域重叠时,memmove()仍然可以正确的处理,不过执行效率上

会比使用memcpy()略慢些。
返回值  返回指向dest的指针。
附加说明  指针src和dest所指的内存区域可以重叠
范例  参考memcpy()。
 
 
5.10 memset(将一段内存空间填入某值) 
相关函数  bzero,swab
表头文件  #include<string.h>
定义函数  void * memset (void *s ,int c, size_t n);
函数说明  memset()会将参数s所指的内存区域前n个字节以参数c填入,然后返回指向s的指针。在编写

程序时,若需要将某一数组作初始化,memset()会相当方便。
返回值  返回指向s的指针。
附加说明  参数c虽声明为int, 但必须是unsigned char ,所以范围在0到255之间。
范例  #include <string.h>
main()
{
char s[30];
memset (s,'A',sizeof(s));
s[30]='/0';
printf("%s/n",s);
}
 
执行  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
 

5.11 rindex(查找字符串中最后一个出现的指定字符) 
相关函数  index,memchr,strchr,strrchr
表头文件  #include<string.h>
定义函数  char * rindex( const char *s,int c);
函数说明  rindex()用来找出参数s字符串中最后一个出现的参数c地址,然后将该字符出现的地址返回。

字符串结束字符(NULL)也视为字符串一部分。
返回值  如果找到指定的字符则返回该字符所在的地址,否则返回0。
范例  #include <string.h>
mian()
{
char *s ="0123456789012345678901234567890";
char *p;
p=rindex(s,'5');
printf("%s/n",p);
}
 
执行  567890
 
 
5.12 strcasecmp(忽略大小写比较字符串) 
相关函数  bcmp,memcmp,strcmp,strcoll,strncmp
表头文件  #include<string.h>
定义函数  int strcasecmp (const char *s1, const char *s2);
函数说明  strcasecmp()用来比较参数s1和s2字符串,比较时会自动忽略大小写的差异。
返回值  若参数s1和s2字符串相同则返回0。s1长度大于s2长度则返回大于0 的值,s1 长度若小于s2 长

度则返回小于0的值。
范例  #include <string.h>
main()
{
char *a="aBcDeF";
char *b="AbCdEf";
if(!strcasecmp(a,b))
printf("%s=%s/n",a,b);
}
 
执行  aBcDeF=AbCdEf
 

5.13 strcat(连接两字符串) 
相关函数  bcopy,memccpy,memcpy,strcpy,strncpy
表头文件  #include <string.h>
定义函数  char *strcat (char *dest,const char *src);
函数说明  strcat()会将参数src字符串拷贝到参数dest所指的字符串尾。第一个参数dest要有足够的空间

来容纳要拷贝的字符串。
返回值  返回参数dest的字符串起始地址
范例  #include <string.h.>
main()
{
char a[30]="string(1)";
char b[]="string(2)";
printf("before strcat() : %s/n",a);
printf("after strcat() : %s/n",strcat(a,b));
}
 
执行  before strcat () : string(1)
after strcat () : string(1)string(2)
 
 
5.14 strchr(查找字符串中第一个出现的指定字符) 
相关函数  index,memchr,rinex,strbrk,strsep,strspn,strstr,strtok
表头文件  #include<string.h>
定义函数  char * strchr (const char *s,int c);
函数说明  strchr()用来找出参数s字符串中第一个出现的参数c地址,然后将该字符出现的地址返回。
返回值  如果找到指定的字符则返回该字符所在地址,否则返回0。
范例  #include<string.h>
main()
{
char *s=0123456789012345678901234567890”;
char *p;
p=strchr(s,'5');
printf("%s/n",p);
}
 
执行  5.68E+25
 
 
5.15 strcmp(比较字符串) 
相关函数  bcmp,memcmp,strcasecmp,strncasecmp,strcoll
表头文件  #include<string.h>
定义函数  int strcmp(const char *s1,const char *s2);
函数说明  strcmp()用来比较参数s1和s2字符串。字符串大小的比较是以ASCII 码表上的顺序来决定,此

顺序亦为字符的值。strcmp()首先将s1第一个字符值减去s2第一个字符值,若差值为0则再继续比较下

个字符,若差值不为0则将差值返回。例如字符串"Ac"和"ba"比较则会返回字符"A"(65)和'b'(98)的差值(

-33)。
返回值  若参数s1和s2字符串相同则返回0。s1若大于s2则返回大于0的值。s1若小于s2则返回小于0 的

值。
 
范例  #include<string.h>
main()
{
char *a="aBcDeF";
char *b="AbCdEf";
char *c="aacdef";
char *d="aBcDeF";
printf("strcmp(a,b) : %d/n",strcmp(a,b));
printf("strcmp(a,c) : %d/n",strcmp(a,c));
printf("strcmp(a,d) : %d/n",strcmp(a,d));
}
 
执行  strcmp(a,b) : 32
strcmp(a,c) :-31
strcmp(a,d) : 0
 
 
5.16 strcoll(采用目前区域的字符排列次序来比较字符串) 
相关函数  strcmp,bcmp,memcmp,strcasecmp,strncasecmp
表头文件  #include<string.h>
定义函数  int strcoll( const char *s1, const char *s2);
函数说明  strcoll()会依环境变量LC_COLLATE所指定的文字排列次序来比较s1和s2 字符串。
返回值  若参数s1和s2字符串相同则返回0。s1若大于s2则返回大于0的值。s1若小于s2则返回小于0 的

值。
附加说明  若LC_COLLATE为"POSIX"或"C",则strcoll()与strcmp()作用完全相同。
范例  参考strcmp()。
 

5.17 strcpy(拷贝字符串) 
相关函数  bcopy,memcpy,memccpy,memmove
表头文件  #include<string.h>
定义函数  char *strcpy(char *dest,const char *src);
函数说明  strcpy()会将参数src字符串拷贝至参数dest所指的地址。
返回值  返回参数dest的字符串起始地址。
附加说明  如果参数dest所指的内存空间不够大,可能会造成缓冲溢出(buffer Overflow)的错误情况,在

编写程序时请特别留意,或者用strncpy()来取代。
范例  #include<string.h>
main()
{
char a[30]="string(1)";
char b[]="string(2)";
printf("before strcpy() :%s/n",a);
printf("after strcpy() :%s/n",strcpy(a,b));
}
 
执行  before strcpy() :string(1)
after strcpy() :string(2)
 
 
5.18 strcspn(返回字符串中连续不含指定字符串内容的字符数) 
相关函数  strspn
表头文件  #inclued<string.h>
定义函数  size_t strcspn ( const char *s,const char * reject);
函数说明  strcspn()从参数s字符串的开头计算连续的字符,而这些字符都完全不在参数reject 所指的字

符串中。简单地说,若strcspn()返回的数值为n,则代表字符串s开头连续有n个字符都不含字符串reject

内的字符。
返回值  返回字符串s开头连续不含字符串reject内的字符数目。
范例  #include <string.h>
main()
{
char *str="Linux was first developed for 386/486-based pcs.";
printf("%d/n",strcspn(str," "));
printf("%d/n",strcspn(str,"/-"));
printf("%d/n",strcspn(str,"1234567890"));
}
 
执行  5 /*只计算到“ ”的出现,所以返回“Linux”的长度*/
33 /*计算到出现“/”或“-”,所以返回到“6”的长度*/
30 /* 计算到出现数字字符为止,所以返回“3”出现前的长度*/
 

5.19 strdup(复制字符串) 
相关函数  calloc,malloc,realloc,free
表头文件  #include<string.h>
定义函数  char * strdup( const char *s);
函数说明  strdup()会先用maolloc()配置与参数s字符串相同的空间大小,然后将参数s字符串的内容复制

到该内存地址,然后把该地址返回。该地址最后可以利用free()来释放。
返回值  返回一字符串指针,该指针指向复制后的新字符串地址。若返回NULL表示内存不足。
范例  #include<string.h>
main()
{
char a[]="strdup";
char *b;
b=strdup(a);
printf("b[ ]=/"%s/"/n",b);
}
 
执行  b[ ]="strdup"
 
 
5.20 strlen(返回字符串长度) 
相关函数 
表头文件  #include<string.h>
定义函数  size_t strlen (const char *s);
函数说明  strlen()用来计算指定的字符串s的长度,不包括结束字符"/0"。
返回值  返回字符串s的字符数。
范例  /*取得字符串str的长度*/
#include<string.h>
main()
{
char *str = "12345678";
printf("str length = %d/n", strlen(str));
}
 
执行  str length = 8
 
 
5.21 strncasecmp(忽略大小写比较字符串) 
相关函数  bcmp,memcmp,strcmp,strcoll,strncmp
表头文件  #include<string.h>
定义函数  int strncasecmp(const char *s1,const char *s2,size_t n);
函数说明  strncasecmp()用来比较参数s1和s2字符串前n个字符,比较时会自动忽略大小写的差异。
返回值  若参数s1和s2 字符串相同则返回0。s1 若大于s2则返回大于0的值,s1若小于s2则返回小于0

的值。
范例  #include<string.h>
main()
{
char *a="aBcDeF";
char *b="AbCdEf";
if(!strncasecmp(a,b))
printf("%s =%s/n",a,b);
}
 
执行  aBcDef=AbCdEf
 
 
5.22 strncat(连接两字符串) 
相关函数  bcopy,memccpy,memecpy,strcpy,strncpy
表头文件  #inclue <string.h>
定义函数  char * strncat(char *dest,const char *src,size_t n);
函数说明  strncat()会将参数src字符串拷贝n个字符到参数dest所指的字符串尾。第一个参数dest要有足

够的空间来容纳要拷贝的字符串。
返回值  返回参数dest的字符串起始地址。
范例  #include <string.h>
main()
{
char a[30]="string(1)";
char b[]="string(2)";
printf("before strnact() :%s/n", a);
printf("after strncat() :%s/n", strncat(a,b,6));
}
 
执行  before strnact() : string(1)
after strncat() : string(1) string
 

5.23 strncpy(拷贝字符串) 
相关函数  bcopy,memccpy,memcpy,memmove
表头文件  #include<string.h>
定义函数  char * strncpy(char *dest,const char *src,size_t n);
函数说明  strncpy()会将参数src字符串拷贝前n个字符至参数dest所指的地址。
返回值  返回参数dest的字符串起始地址。
范例  #inclue <string.h>
main()
{
char a[30]="string(1)";
char b[]="string(2)";
printf("before strncpy() : %s/n",a);
printf("after strncpy() : %s/n",strncpy(a,b,6));
}
 
执行  before strncpy() : string(1)
after strncpy() : string(1)
 
 
5.24 strpbrk(查找字符串中第一个出现的指定字符) 
相关函数  index,memchr,rindex,strpbrk,strsep,strspn,strstr,strtok
表头文件  #include <include.h>
定义函数  char *strpbrk(const char *s,const char *accept);
函数说明  strpbrk()用来找出参数s 字符串中最先出现存在参数accept 字符串中的任意字符。
返回值  如果找到指定的字符则返回该字符所在地址,否则返回0。
范例  #include <string.h>
main()
{
char *s="0123456789012345678901234567890";
char *p;
p=strpbrk(s,"a1 839"); /*1会最先在s字符串中找到*/
printf("%s/n",p);
p=strprk(s,"4398");/*3 会最先在s 字符串中找到*/
printf("%s/n",p);
 
执行  1.23E+29
 
  
5.25 strrchr(查找字符串中最后出现的指定字符) 
相关函数  index,memchr,rindex,strpbrk,strsep,strspn,strstr,strtok
表头文件  #include<string.h>
定义函数  char * strrchr(const char *s, int c);
函数说明  strrchr()用来找出参数s字符串中最后一个出现的参数c地址,然后将该字符出现的地址返回。
返回值  如果找到指定的字符则返回该字符所在地址,否则返回0。
范例  #include<string.h>
main()
{
char *s="0123456789012345678901234567890";
char *p;
p=strrchr(s,'5');
printf("%s/n",p);
}
 
执行  567890
 
 
5.26 strspn(返回字符串中连续不含指定字符串内容的字符数) 
相关函数  strcspn,strchr,strpbrk,strsep,strstr
表头文件  #include<string.h>
定义函数  size_t strspn (const char *s,const char * accept);
函数说明  strspn()从参数s 字符串的开头计算连续的字符,而这些字符都完全是accept 所指字符串中的

字符。简单的说,若strspn()返回的数值为n,则代表字符串s 开头连续有n 个字符都是属于字符串

accept内的字符。
返回值  返回字符串s开头连续包含字符串accept内的字符数目。
范例  #include<string.h>
main()
{
char *str="Linux was first developed for 386/486-based PCs.";
char *t1="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d/n",strspn(str,t1));
}
 
执行  5 /*计算大小写字母。不包含“ ”,所以返回Linux的长度。*/
 
 
5.27 strstr(在一字符串中查找指定的字符串) 
相关函数  index,memchr,rindex,strchr,strpbrk,strsep,strspn,strtok
表头文件  #include<string.h>
定义函数  char *strstr(const char *haystack,const char *needle);
函数说明  strstr()会从字符串haystack 中搜寻字符串needle,并将第一次出现的地址返回。
返回值  返回指定字符串第一次出现的地址,否则返回0。
范例  #include<string.h>
main()
{
char * s="012345678901234567890123456789";
char *p;
p= strstr(s,"901");
printf("%s/n",p);
}
 
执行  9.01E+21
 
 
5.28 strtok(分割字符串) 
相关函数  index,memchr,rindex,strpbrk,strsep,strspn,strstr
表头文件  #include<string.h>
定义函数  char * strtok(char *s,const char *delim);
函数说明  strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字

符串,当strtok()在参数s的字符串中发现到参数delim的分割字符时则会将该字符改为/0 字符。在第一次

调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回下一

个分割后的字符串指针。
返回值  返回下一个分割后的字符串指针,如果已无从分割则返回NULL。
范例  #include<string.h>
main()
{
char s[]="ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z";
char *delim="-: ";
char *p;
printf("%s ";strtok(s,delim));
while((p=strtok(NULL,delim)))printf("%s ",p);
printf("/n");
}
 
执行  ab cd ef;gh i jkl;mnop;qrs tu vwx y;z /*-与:字符已经被/0 字符取代*/
 


6. 常用数学函数篇
6.1 abs(计算整型数的绝对值) 
相关函数  labs, fabs
表头文件  #include<stdlib.h>
定义函数  int abs (int j)
函数说明  abs()用来计算参数j的绝对值,然后将结果返回。
返回值  返回参数j的绝对值结果。
范例  #ingclude <stdlib.h>
main(){
int ansert;
answer = abs(-12);
printf("|-12| = %d/n", answer);
}
 
执行  |-12| = 12
 

6.2 acos(取反余弦函数数值) 
相关函数  asin , atan , atan2 , cos , sin , tan
表头文件  #include <math.h>
定义函数  double acos (double x);
函数说明  acos()用来计算参数x的反余弦值,然后将结果返回。参数x范围为-1至1之间,超过此范围

则会失败。
返回值  返回0至PI之间的计算结果,单位为弧度,在函数库中角度均以弧度来表示。
错误代码  EDOM参数x超出范围。
附加说明  使用GCC编译时请加入-lm。
范例  #include <math.h>
main (){
double angle;
angle = acos(0.5);
printf("angle = %f/n", angle);
}
 
执行  angle = 1.047198
 
 
6.3 asin(取反正弦函数值) 
相关函数  acos , atan , atan2 , cos , sin , tan
表头文件  #include <math.h>
定义函数  double asin (double x)
函数说明  asin()用来计算参数x的反正弦值,然后将结果返回。参数x范围为-1至1之间,超过此范围则

会失败。
返回值  返回-PI/2之PI/2之间的计算结果。
错误代码  EDOM参数x超出范围
附加说明  使用GCC编译时请加入-lm
范例  #include<math.h>
main()
{
double angle;
angle = asin (0.5);
printf("angle = %f/n",angle);
}
 
执行  angle = 0.523599
 
 
6.4 atan(取反正切函数值) 
相关函数  acos,asin,atan2,cos,sin,tan
表头文件  #include<math.h>
定义函数  double atan(double x);
函数说明  atan()用来计算参数x的反正切值,然后将结果返回。
返回值  返回-PI/2至PI/2之间的计算结果。
附加说明  使用GCC编译时请加入-lm
范例  #include<math.h>
main()
{
double angle;
angle =atan(1);
printf("angle = %f/n",angle);
}
 
执行  angle = 1.570796
 
 
6.5 atan2(取得反正切函数值) 
相关函数  acos,asin,atan,cos,sin,tan
表头文件  #include<math.h>
定义函数  double atan2(double y,double x);
函数说明  atan2()用来计算参数y/x的反正切值,然后将结果返回。
返回值  返回-PI/2 至PI/2 之间的计算结果
附加说明  使用GCC编译时请加入-lm。
范例  #include<math.h>
main()
{
double angle;
angle = atan2(1,2);
printf("angle = %f/n", angle);
}
 
执行  angle = 0.463648
 
 
6.6 ceil(取不小于参数的最小整型数) 
相关函数  fabs
表头文件  #include <math.h>
定义函数  double ceil (double x);
函数说明  ceil()会返回不小于参数x的最小整数值,结果以double形态返回。
返回值  返回不小于参数x的最小整数值。
附加说明  使用GCC编译时请加入-lm。
范例  #include<math.h>
main()
{
double value[ ]={4.8,1.12,-2.2,0};
int i;
for (i=0;value[i]!=0;i++)
printf("%f=>%f/n",value[i],ceil(value[i]));
}
 
执行  4.800000=>5.000000
1.120000=>2.000000
-2.200000=>-2.000000
 
 
6.7 cos(取余玄函数值) 
相关函数  acos,asin,atan,atan2,sin,tan
表头文件  #include<math.h>
定义函数  double cos(double x);
函数说明  cos()用来计算参数x 的余玄值,然后将结果返回。
返回值  返回-1至1之间的计算结果。
附加说明  使用GCC编译时请加入-lm。
范例  #include<math.h>
main()
{
double answer = cos(0.5);
printf("cos (0.5) = %f/n",answer);
}
 
执行  cos(0.5) = 0.877583
 
 
6.8 cosh(取双曲线余玄函数值) 
相关函数  sinh,tanh
表头文件  #include<math.h>
定义函数  double cosh(double x);
函数说明  cosh()用来计算参数x的双曲线余玄值,然后将结果返回。数学定义式为:(exp(x)+exp(-x))/2。
返回值  返回参数x的双曲线余玄值。
附加说明  使用GCC编译时请加入-lm。
范例  #include<math.h>
main()
{
double answer = cosh(0.5);
printf("cosh(0.5) = %f/n",answer);
}
 
执行  cosh(0.5) = 1.127626
 
 
6.9 exp(计算指数) 
相关函数  log,log10,pow
表头文件  #include<math.h>
定义函数  double exp(double x);
函数说明  exp()用来计算以e为底的x次方值,即ex值,然后将结果返回。
返回值  返回e的x次方计算结果。
附加说明  使用GCC编译时请加入-lm。
范例  #include<math.h>
main()
{
double answer;
answer = exp (10);
printf("e^10 =%f/n", answer);
}
 
执行  e^10 = 22026.465795
 
 
6.10 frexp(将浮点型数分为底数与指数) 
相关函数  ldexp,modf
表头文件  #include<math.h>
定义函数  double frexp( double x, int *exp);
函数说明  frexp()用来将参数x 的浮点型数切割成底数和指数。底数部分直接返回,指数部分则借参数

exp 指针返回,将返回值乘以2 的exp次方即为x的值。
返回值  返回参数x的底数部分,指数部分则存于exp指针所指的地址。
附加说明  使用GCC编译时请加入-lm。
范例  #include <math.h>
main()
{
int exp;
double fraction;
fraction = frexp (1024,&exp);
printf("exp = %d/n",exp);
printf("fraction = %f/n", fraction);
}
 
执行  exp = 11
fraction = 0.500000 /* 0.5*(2^11)=1024*/
 
 
6.11 ldexp(计算2的次方值) 
相关函数  frexp
表头文件  #include<math.h>
定义函数  double ldexp(double x,int exp);
函数说明  ldexp()用来将参数x乘上2的exp次方值,即x*2exp。
返回值  返回计算结果。
附加说明  使用GCC编译时请加入-lm。
范例:  /* 计算3*(2^2)=12 */
#include<math.h>
main()
{
int exp;
double x,answer;
answer = ldexp(3,2);
printf("3*2^(2) = %f/n",answer);
}
 
执行  3*2^(2) = 12.000000
 
 
6.12 log(计算以e 为底的对数值) 
相关函数  exp,log10,pow
表头文件  #include <math.h>
定义函数  double log (double x);
函数说明  log()用来计算以e为底的x 对数值,然后将结果返回。
返回值  返回参数x的自然对数值。
错误代码  EDOM 参数x为负数,ERANGE 参数x为零值,零的对数值无定义。
附加说明  使用GCC编译时请加入-lm。
范例  #include<math.h>
main()
{
double answer;
answer = log (100);
printf("log(100) = %f/n",answer);
}
 
执行  log(100) = 4.605170
 
 
6.13 log10(计算以10 为底的对数值) 
相关函数  exp,log,pow
表头文件  #include<math.h>
定义函数  double log10(double x);
函数说明  log10()用来计算以10为底的x对数值,然后将结果返回。
返回值  返回参数x以10为底的对数值。
错误代码  EDOM参数x为负数。RANGE参数x为零值,零的对数值无定义。
附加说明  使用GCC编译时请加入-lm。
范例  #include<math.h>
main()
{
double answer;
answer = log10(100);
printf("log10(100) = %f/n",answer);
}
 
执行  log10(100) = 2.000000
 
 
6.14 pow(计算次方值) 
相关函数  exp,log,log10
表头文件  #include<math.h>
定义函数  double pow(double x,double y);
函数说明  pow()用来计算以x为底的y次方值,即xy值,然后将结果返回。
返回值  返回x的y次方计算结果。
错误代码  EDOM 参数x为负数且参数y不是整数。
附加说明  使用GCC编译时请加入-lm。
范例  #include <math.h>
main()
{
double answer;
answer =pow(2,10);
printf("2^10 = %f/n", answer);
}
 
执行  2^10 = 1024.000000
 
 
6.15 sin(取正玄函数值) 
相关函数  acos,asin,atan,atan2,cos,tan
表头文件  #include<math.h>
定义函数  double sin(double x);
函数说明  sin()用来计算参数x的正玄值,然后将结果返回。
返回值  返回-1 至1之间的计算结果。
附加说明  使用GCC编译时请加入-lm。
范例  #include<math.h>
main()
{
double answer = sin (0.5);
printf("sin(0.5) = %f/n",answer);
}
 
执行  sin(0.5) = 0.479426
 

6.16 sinh(取双曲线正玄函数值) 
相关函数  cosh,tanh
表头文件  #include<math.h>
定义函数  double sinh( double x);
函数说明  sinh()用来计算参数x的双曲线正玄值,然后将结果返回。数学定义式为:(exp(x)-exp(-x))/2。
返回值  返回参数x的双曲线正玄值。
附加说明  使用GCC编译时请加入-lm。
范例  #include<math.h>
main()
{
double answer = sinh (0.5);
printf("sinh(0.5) = %f/n",answer);
}
 
执行  sinh(0.5) = 0.521095
 
 
6.17 sqrt(计算平方根值) 
相关函数  hypotq
表头文件  #include<math.h>
定义函数  double sqrt(double x);
函数说明  sqrt()用来计算参数x的平方根,然后将结果返回。参数x必须为正数。
返回值  返回参数x的平方根值。
错误代码  EDOM 参数x为负数。
附加说明  使用GCC编译时请加入-lm。
范例  /* 计算200的平方根值*/
#include<math.h>
main()
{
double root;
root = sqrt (200);
printf("answer is %f/n",root);
}
 
执行  answer is 14.142136
  

6.18 tan(取正切函数值) 
相关函数  atan,atan2,cos,sin
表头文件  #include <math.h>
定义函数  double tan(double x);
函数说明  tan()用来计算参数x的正切值,然后将结果返回。
返回值  返回参数x的正切值。
附加说明  使用GCC编译时请加入-lm。
范例  #include<math.h>
main()
{
double answer = tan(0.5);
printf("tan (0.5) = %f/n",answer);
}
 
执行  tan(0.5) = 0.546302
 
 
6.19 tanh(取双曲线正切函数值) 
相关函数  cosh,sinh
表头文件  #include<math.h>
定义函数  double tanh(double x);
函数说明  tanh()用来计算参数x的双曲线正切值,然后将结果返回。数学定义式为:sinh(x)/cosh(x)。
返回值  返回参数x的双曲线正切值。
附加说明  使用GCC编译时请加入-lm。
范例  #include<math.h>
main()
{
double answer = tanh(0.5);
printf("tanh(0.5) = %f/n",answer);
}
 
执行  tanh(0.5) = 0.462117

原创粉丝点击