ANSI C (3) —— 常用系统函数
来源:互联网 发布:管理信息系统软件测试 编辑:程序博客网 时间:2024/06/06 09:27
字符测试函数
简单例子:
#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.*/
查找
系统时间和日期函数
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
环境控制函数
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;}
内存分配函数
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
- ANSI C (3) —— 常用系统函数
- 常用函数unicode ansi
- ANSI C (2) —— str系列函数
- ANSI C——printf
- 黑马程序员—C语言概述常用系统函数
- 常用ANSI C标准库函数
- ANSI C文件操作函数
- c语言常用系统函数
- 常用系统函数C语言
- C++与ANSI C函数原型差异——第七章(P186)
- ANSI-C面向对象编程翻译:第二章 动态连接——通用函数
- ANSI C——可变参数
- ANSI C —— 格式化输出
- 堆排序——ANSI C实现
- ANSI C (1) —— 基础
- ANSI C 库最常用的几个处理字符串的函数
- C++ UNICODE ANSI 类型转换 常用函数
- C语言学习——ANSI C标准函数库
- Android高级控件(四)——VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷
- 控制结构
- Twins
- 电源设计调试过程中的异常现象介绍
- eclipse
- ANSI C (3) —— 常用系统函数
- 用c语言实现三子棋的游戏
- bash基础知识
- 51nod 1640 天气晴朗的魔法 By Assassin
- Word Capitalization
- 手机上设置图片需要注意的问题
- 知识杂选
- js播放图片序列帧
- 二叉树的实现