ANSI C (3) —— 常用系统函数

来源:互联网 发布:管理信息系统软件测试 编辑:程序博客网 时间:2024/06/06 09:27

字符测试函数

function effect isalnum 检测字符是否为英文或数字 isalpha 检测字符是否为英文 isascii 检测字符是否为ASCII码字符 iscntrl 检测字符是否为ASCII码控制字符 isdigit 检测字符是否为阿拉伯数字 islower 检测字符是否为小写字符 isupper 检测字符是否为大写字符 isprint 检测字符是否为可打印字符 isspace 检测字符是否为空格字符 ispunct 检测字符是否是标点符号或特殊符号 isxdigit 检测字符是否是16进制数字

简单例子:

#include <stdio.h>#include <stdlib.h>#include <ctype.h>int main(){    char *s = "Hello world.";    while(*s){        if(isupper(*s)) printf("%c is upper character.\n",*s);        else if(islower(*s)) printf("%c is lower character.\n",*s);        else if(isspace(*s)) printf("%c is space character.\n",*s);        else if(ispunct(*s)) printf("%c is punction mark.\n",*s);        s++;    }    return 0;}/*H is upper character.e is lower character.l is lower character.l is lower character.o is lower character.  is space character.w is lower character.o is lower character.r is lower character.l is lower character.d is lower character.. is punction mark.*/

查找

function format effect bsearch void bsearch(const void *key, const void *base, size_t *nelem, size_t width,int(*fcmp)(const void , const *)) 使用二分查找的方法寻找目标值 qsort void qsort(void base, int nelem, int width, int(*fcmp)(const void , const *)) 快速排序算法得到有序序列 lfind void lfind(void *key, void *base, int *nelem, int width,int (*fcmp)(const void , const void *)) 线性搜索 lsearch void lsearch(const void *key, void *base, size_t *nelem,size_t width, int (*fcmp)(const void , const void *)) 线性搜索,找不到的话将目标数据加入数组

系统时间和日期函数

function format effect clock clock_t clock(void) 返回处理器时间的最佳近似值 time time_t time(time_t *tp) 获取系统时间 ctime char *ctime(const time_t *time) time_t日历时间转换为字符串形式的本地时间 difftime double difftime(time_t time2, time_t time1) 计算时间差函数 gmtime struct tm *gmtime(const time_t *timer) 将日历时间转换为 GMT localtime struct tm *localtime(const time_t *timer) 时间类型转换函数 mktime time_t mktime(struct tm*timeptr) 时间类型转换函数 asctime char *asctime(const struct tm *tblock) 把指定的 tm结构类的日期转换成字符串,如Mon Nov 21 11:31:54 1983

clock

clock_t是long替换,程序从启动到函数调用占用CPU的时间,在MSDN中称之为挂钟时间(wal-clock);若挂钟时间不可取,则返回-1。其中clock_t是用来保存时间的数据类型

#include <stdio.h>#include <stdlib.h>#include <time.h>int main(){    clock_t start = clock();    sleep(1);  /* 将程序挂起1秒 */    clock_t end = clock();    printf("spent time is %.8lf\n",(double)(end-start)/CLOCKS_PER_SEC);    return 0;}/*./timespent time is 0.00000000*/

分析:在程序挂起的时间段内,进程被调离出内存,所以没有占用cpu,那么程序的运行结果也就是0了。
换一个例子:

#include <stdio.h>#include <stdlib.h>#include <time.h>#include <signal.h>int global;void stop(int sig){    printf("ctrl + c change global to 1.\n");    global=1;}int main(){    signal(SIGINT,stop);    clock_t start = clock();    while(global == 0);    clock_t end = clock();    printf("spent time is %.8lf\n",(double)(end-start)/CLOCKS_PER_SEC);    return 0;}/*/time2^Cctrl + c change global to 1.spent time is 1.56000000*/

time, ctime

在/usr/include/time.h中有这样一句话:
typedef __time_t time_t;
并且在它的上面还有一句话: # include <bits/types.h>
在此文件中,我们搜索到:__STD_TYPE __TIME_T_TYPE __time_t; /* Seconds since the Epoch. */

这说明time_t是一种时间类型,存放自1970年1月1日0点0时0分开始的秒数

#include <stdio.h>#include <stdlib.h>#include <time.h>int main(){    time_t t;    time(&t);    printf("%d\n",t); /* 32 bit integer */    printf("%s\n",ctime(&t));     return 0;}/*./time31477848041Mon Oct 31 01:20:41 2016*/

difftime

因为time获取的是系统时间,和CPU占用没有关系,所以这里的sleep()达到了效果(和clock的例子作比较)。

#include <stdio.h>#include <stdlib.h>#include <time.h>int main(){    time_t t1 = time(NULL); /* get system time */    sleep(1);    time_t t2 = time(NULL);    printf("spent time is %.8lf\n",difftime(t2,t1));    return 0;}

gmtime asctime

struct tm的定义是这样的:

/* /usr/include/time.h  */struct tm{  int tm_sec;           /* Seconds. [0-60] (1 leap second) */  int tm_min;           /* Minutes. [0-59] */  int tm_hour;          /* Hours.   [0-23] */  int tm_mday;          /* Day.     [1-31] */  int tm_mon;           /* Month.   [0-11] */  int tm_year;          /* Year - 1900.  */  int tm_wday;          /* Day of week. [0-6] */  int tm_yday;          /* Days in year.[0-365] */  int tm_isdst;         /* DST.     [-1/0/1]*/# ifdef __USE_MISC  long int tm_gmtoff;       /* Seconds east of UTC.  */  const char *tm_zone;      /* Timezone abbreviation.  */# else  long int __tm_gmtoff;     /* Seconds east of UTC.  */  const char *__tm_zone;    /* Timezone abbreviation.  */# endif};

下面的例子是:

graph TDA[time_t]-->B[struct tm]B-->|asctime|C[GMT time string]A-->|ctime|C
#include <stdio.h>#include <stdlib.h>#include <time.h>int main(){    time_t t = time(NULL);    struct tm *gmt=gmtime(&t);    printf("time is %s\nGMT time is %s\n",ctime(&t),asctime(gmt));    return 0;}/*./gmtime time is Mon Oct 31 21:20:44 2016GMT time is Mon Oct 31 21:20:44 2016*/

localtime, mktime

graph TDA[struct tm]-->|mktime|B[time_t]B-->|localtime|A

环境控制函数

function format effect getenv char *getenv(char *envvar) 获取环境变量的内容 putenv int putenv(char *envvar) 改变或增加环境变量的内容 setenv int setenv(const char name,const char value,int overwrite) 改变或增加环境变量的内容

getenv

#include <stdio.h>#include <stdlib.h>int main(){    char *p = getenv("USER");    if(p){        printf("USER = %s\n",p);    }    return 0;}/*$ ./getenv USER = edemon*/

putenv

#include <stdio.h>#include <stdlib.h>int main(){    char *p = "pet=dog";    putenv(p);    printf("pet is %s\n",getenv("pet"));    return 0;}

putenv不会对环境变量文件产生影响

/etc/environment /etc/profile/etc/bash.bashrc

setenv

#include <stdio.h>#include <stdlib.h>int main(){    putenv("pet=dog");    int ret=0;    ret = setenv("pet","cat",1);    if(ret!=-1){        printf("change success, pet is %s\n",getenv("pet"));    }    return 0;}

内存分配函数

function format effect malloc void *malloc(unsigned size) 内存分配函数 free void free(void *ptr) 释放已经分配的内存 calloc void *calloc(size_t nelem, size_t elsize) 分配主存储器 getpagesize size_t getpagesize(void) 返回系统内存分页的大小 mmap void* mmap(void* start,size_t length,int prot,int flags,int fd,off_t offset) 将一个文件或者其它对象映射进内存 munmap int munmap(void *start,size_t length) 解除内存映射

calloc

calloc申请内存空间后,会自动初始化内存空间为0:

#include <iostream>#include <stdlib.h>#include <stdio.h>using namespace std;int main() {    // void *calloc(size_t n, size_t size);    int NUM =10;    int *p1 = (int *)calloc(NUM,sizeof(int));    int i;    for(i=0;i<NUM;i++){        printf("%4d",p1[i]);    }    puts("");    if(p1) free(p1);    return 0;}//   0   0   0   0   0   0   0   0   0   0

malloc申请的内存是不会被初始化的。

getpagesize

#include <stdio.h>#include <unistd.h>int main(){    printf("page size is %d byte\n",getpagesize());    return 0;}/*$ ./pagesizepage size is 4096 byte$ uname -aLinux ubuntu 4.2.0-42-generic #49-Ubuntu SMP Tue Jun 28 21:26:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux*/

mmap, munmap

#include <stdio.h>#include <unistd.h>#include <sys/mman.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <errno.h>int main(){    int fd=open("/etc/passwd",O_RDONLY);    if(fd == -1){        perror("open ");        exit(1);    }    struct stat info;    fstat(fd,&info);    /* start set NULL, means system set it. */    /* PROT_READ means memory can be read. */    /* MAP_PRIVATE means create a copy file, not influce origin file. */    void *start=mmap(NULL,info.st_size,PROT_READ,MAP_PRIVATE,fd,0);    if(start == MAP_FAILED) {        perror("mmap ");        exit(1);    }    printf("%s",(char *)start);    munmap(start,info.st_size);    close(fd);    return 0;}
0 0
原创粉丝点击