标准函数库

《C与指针》读书笔记
1 整型函数
1.1 算术<stdlib.h>
int abs(int value);
long int labs(long int value);
div_t div(int numerator,denominator);
ldiv_t ldiv(long int numer,long int denom);
abs函数返回它的参数的绝对值。
labs用于执行相同的任务,但它的作用对象是长整型。
div函数把它的第二个参数(分母)除以第一个参数(分子),产生商和余数,用一个div_t结构返回。这个结构包含下面两个字段,
int quot;//商
int rem;//余数
ldiv函数跟div执行相同的任务,但它的作用对象是长整型。
1.2 随机数<stdlib.h>
int rand(void);
int srand(unsigned int seed);
rand返回一个范围在0和RAND_MAX之间的伪随机数。
为了避免程序每次运行时获得相同的随机数序列,可以调用srand函数。
一个常用的技巧是使用每天的时间作为随机数产生器的种子(seed),如:
srand((unsigned int)time(0));
1.3 字符串转换
int atoi(char const *string);
long int atol(char const *string);
long int strtol(char const *string,char **unused,int base);
unsigned long int strtoul(char const *string,char unused,int base);
atoi和atol分别把字符转换成整型和长整型。strtol和atol同样把参数字符串转换为long。但是,strtol保存了一个指向转换值后面第一个字符的指针。如果函数的第二个参数并非NULL,这个指针就保存在第二个参数所指向的位置。strtoul和strtol的执行方式相同,但它产生一个无符号长整型。
2 浮点型函数
2.1 三角函数<math.h>
double sin(double angle);
double cos(double angle);
double tan(double angle);
double asin(double value);
double acos(double value);
double atan(double value);
double atan2(double x,double y);
2.2 双曲函数<math.h>
double sinh(double angle);
double cosh(double angle);
double tanh(double angle);
2.3 对数和指数函数<math.h>
double exp(double x);
double log(double x);
double log10(double x);
2.4 浮点表示形式<math.h>
double frexp(double value,int *exponent);
double ldexp(double fraction,int exponent);
double modf(double value,double *ipart);
frexp函数计算一个指数和小树,这样fraction * 2^exponent = value,其中0.5<= fraction <1,exponent是一个整数。exponent存储于第二个参数所指向的内存位置,函数返回fraction的值。
ldexp函数返回值是fraction * 2^exponent,也就是上面的value。frexp和ldexp用在浮点格式不兼容的机器之间进行浮点数传递。
modf函数把一个浮点值分成整数和小数两个部分,每个部分都具有和原值一样的符号。整数部分以double类型存储于第2个参数所指向的内存位置。
2.5 幂<math.h>
double pow(double x,double y);
double sqrt(double x);
pow函数返回x^y的值。
sqrt函数返回参数的平方根。
2.6 底数、顶数、绝对值和余数<math.h>
double floor(double x);
double ceil(double x);
double fabs(double x);
double fmod(double x,double y);
floor函数返回不大于其参数的最大整数值。
ceil函数返回不小于其参数的最小整数值。
fabs函数返回其参数的绝对值。
fmod函数返回x除以y所产生的余数。这个除法的商被限定是整数值。
2.7 字符串转换
double atof(char const *string);
double strtod(char const *string,char **unused);
这些函数与整型字符串转换函数类似,只不过它们返回浮点值。
3 日期和时间函数
3.1 处理器时间<time.h>
clock_t clock(void);
clock函数返回从程序开始执行起处理器消耗的时间。
这个值是近似值。可以在main函数开始时调用clock,然后把以后调用clock时所返回的值减去前面的值。
clock函数返回一个数字。转化为秒,除以常量CLOCKS_PER_SEC。
3.2 当天时间<time.h>
(1)time_t time(time_t *returned_value);
time函数返回当前的日期和时间。
如果参数是个非NULL指针,时间值也将通过这个指针进行存储。

日期和时间的转换<time.h>
(2)char *ctime(time_t const *time_value);
ctime函数返回一个字符串指针,格式如下:
Sun Jul 4 04:02:48 1976\n\0
(3)double difftime(time_t time1,time_t time2);
difftime函数计算time1 - time2的差,并把结果转换为秒。
接下来的两个函数把一个time_t值转化为tm结构。
(4)struct tm *gmtime(time_t const *time_value);
gmtime把时间转化为格林尼治时间。
(5)struct tm *localtime(time_t const *time_value);
localtime函数把时间转化成当地时间。
当你拥有一个tm结构之后,可以把它作为参数传给下面两个函数。
(6)char *asctime(struct tm const *tm_ptr);
asctime函数把参数所表示的时间值转换为下面格式:
Sun Jul 4 04:02:48 1976\n\0
这个格式跟ctime一样。
(7)size_t strftime(char *string,size_t maxsize,char const *format,struct tm const *tm_ptr);
strftime函数把一个tm结构转换为一个根据某个格式字符串而定的字符串。
如果转换结果字符串的长度小于maxsize参数,那么该字符串就被复制到第1个参数所指向的数组中。strftime函数返回字符串的长度。否则返回-1。
(8)time_t mktime(struct tm *tm_ptr);
mktime函数用于把tm结构转换为一个time_t值。
4 非本地跳转<setjmp.h>
int setjmp(jmp_buf state);
int longjmp(jmp_buf state,int value);
声明一个jmp_buf变量,并调用setjmp函数对它进行初始化,setjmp的返回值为零。setjmp把程序的状态信息(例如堆栈指针的当前位置和程序的计数器)保存到跳转缓冲区。调用setjmp时所处的函数称为顶层函数。
以后,在顶层函数或其他任何它所调用的函数内的任何地方,调用longjmp函数,将导致这个被保存的状态重新恢复。longjmp的效果就是使执行流通过再次从setjmp函数返回,从而立即跳回到顶层函数中。
实例:
/*
** A program to demonstrate the use of setjmp
*/
#include "trans.h"
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>

/*
** The variable that stores setjmp's state information.
*/
jmp_buf restart;

int
main()
{
int value;
Trans *transaction;

/*
** Establish the point at which we want to resume execution
** after a call to longjmp.
*/
value = setjmp( restart );

/*
** Figure out what to do after a return from setjmp.
*/
switch( value ){
default:
/*
** longjmp was called -- fatal error
*/
fputs( "Fatal error.\n", stderr );
break;

case 1:
/*
** longjmp was called -- minor error
*/
fputs( "Invalid transaction.\n", stderr );
/* FALL THROUGH and continue processing */

case 0:
/*
** Original return from setjmp: perform normal
** processing.
*/
while( (transaction = get_trans()) != NULL )
process_trans( transaction );

}

/*
** Save data and exit the program
*/
write_data_to_file();

return value == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
5 信号
有时候,程序遇到了一些事件并不是程序本身所引发的,比如用户中断程序。程序必须对这类事件作出反应。信号就是用于这种目的。信号表示一种事件,它可能是异步地发生。程序可以调用signal函数,或者忽略这个信号,或者设置一个信号处理函数,当信号发生时程序就调用这个函数。
5.1 信号名<signal.h>
信号 含义
SIGABRT 程序请求异常终止
SIGFPE 发生一个算术错误
SIGILL 检测到非法指令
SIGSEGV 检测到内存的非法访问
SIGINT 收到一个交互性注意信号
SIGTERM 收到一个终止程序请求
前面四个信号是同步的,SIGINT和SIGTERM是异步。
SIGINT信号在绝大多数机器中都是当用户试图中断程序时发生的。SIGTERM则是另一种用户请求终止程序的信号。在实现了这两个信号的系统里,一种常用的策略是为SIGINT定义一个信号处理函数,目的是执行一些日常维护工作并在程序退出前保存数据。但是,SIGTERM则不配备信号处理函数,这样当程序终止时便不必执行这些日常维护工作。
5.2 处理信号
(1)int raise(int sig);
调用这个函数将引发它的参数所指定的信号。可以调用这个函数对信号处理函数进行测试。
(2)void (*signal(int sig,void (*handler)(int)))(int);
signal函数用于指定程序希望采取的反应。
signal是一个函数,它返回一个指针,后者所指向的函数接受一个整型且没有返回值。事实上,signal函数返回一个指向该信号以前的处理函数指针。通过保存这个值,你可以为信号设置一个处理函数并在将来恢复为先前的处理函数。如果调用signal失败,函数将返回SIG_ERR值。
7. 执行环境
7.1 终止执行<stdlib.h>
void abort(void);
void atexit(void (func)(void));
void exit(int status);
abort函数用于不正常地终止一个正在执行的程序。
atexit函数可以把一些函数注册为退出函数。
exit函数用于正常终止程序。
当exit函数被调用时,所有被atexti函数注册为退出函数的函数将按照它们所注册的顺序被反序依次调用。然后,所有用于流的缓冲区被刷新,所有打开的文件被关闭。用tmpfile函数创建的文件被删除。然后,退出状态返回给宿主环境,程序停止执行。
7.2 断言<assert.h>
void assert(int expression);
断言就是声明某种东西应该为真。
7.3 环境<stdlib.h>
环境就是一个由编译器定义的名字/值对的列表,它由操作系统进行维护。
char *getenv(char const *name);
getenv函数在这个列表中查找一个特定的名字,如果找到就返回一个指向其对应值的指针。
7.4 执行系统命令<stdlib.h>
void system(char const *command);
system函数把它的字符串参数传递给宿主操作系统,这样它就可以作为一条命令,由系统的命令处理器执行。
7.5 排序和查找<stdlib.h>
(1)void qsort(void *base,size_t n_elements,size_t el_size,int (*compare)(void const*,void const *));
第一个参数指向需要排序的数组,第二个参数指定数组中元素的数目,第三个参数指定每个元素的长度(以字符为单位)。第四个函数是一个函数指针。
/*
** Demonstrates sorting an array of structures with qsort
*/
#include <stdlib.h>
#include <string.h>

typedef struct {
char key[ 10 ]; /* the sort key for the array */
int other_data; /* data associated with the key */
} Record;

/*
** Comparison function: compares only the key value.
*/
int r_compare( void const *a, void const *b ){
return strcmp( ((Record *)a)->key, ((Record *)b)->key );
}

int
main()
{
Record array[ 50 ];

/*
** Code that fills the array with 50 elements.
*/

qsort( array, 50, sizeof( Record ), r_compare );

/*
** Array is now sorted by the key field of the structures.
*/

return EXIT_SUCCESS;
}
(2)void *bsearch(void const *key,void const *base,size_tn_elements,size_t el_size,int (*compare)(void const*,void const*));
bsearch函数在一个已经排好序的数组中使用二分法查找一个特定的元素。如果数组尚未排序,其结果是未定义的。
第一个参数指向你需要查找的值,第二个参数指向查找所在的数组。第三个参数指定数组中元素的数目,第四个参数是每个元素的长度(以字符为单位)。最后一个是参数是比较函数指针。

/*
** Demonstrates searching an array of structures with bsearch
*/
#include <stdlib.h>
#include <string.h>

typedef struct {
char key[ 10 ]; /* the sort key for the array */
int other_data; /* data associated with the key */
} Record;

/*
** Comparison function: compares only the key value.
*/
int r_compare( void const *a, void const *b ){
return strcmp( ((Record *)a)->key, ((Record *)b)->key );
}

int
main()
{
Record array[ 50 ];
Record key;
Record *ans;

/*
** Code that fills the array with 50 elements and sorts it
*/

/*
** Create a key record (only the key field filled in with the
** value we want to locate) and search the array.
*/
strcpy( key.key, "value" );
ans = bsearch( &key, array, 50, sizeof( Record ),
r_compare );

/*
** ans now points to the array element whose key field
** matches the value, or NULL if none matched.
*/

return EXIT_SUCCESS;
}
8. locale
char *setlocale(int category,char const *locale);
category参数指定locale的哪个部分需要进行修改。
如果setlocale的第二个参数为NULL,函数将返回一个指向给定类型的当前locale的名字的指针。如果第二个参数不是NULL,它指定需要使用的新locale。
修改
LC_ALL整个locale
LC_COLLATE对照序列,它将影响strcoll和strxfrm函数的行为
LC_CTYPE定义与ctype.h中的函数所使用的字符类型分类信息
LC_MONETARY 在格式化货币时使用的字符
LC_NUMERIC 在格式化非货币值时使用的字符。同时修改由格式化输入输出函数和字符串装换函数所使用的小数点符号。
LC_TIMEstrftime函数行为












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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值