C标准库

 

15 Header

  • 合格程序员 ctype.h stdio.h stdlib.h string.h
  • 熟练程序员 assert.h limits.h stddef.h time.h
  • 优秀程序员 float.h math.h error.h locate.h setjmp.h signal.h stdarg.h

assert.h

/* assert.h standard header */
#undef assert

#ifdef NDEBUG
    #define assert(test) ((void) 0)
#else
    void _Assert(char *);
    #define _STR(x) _VAL(x)
    #define _VAL(x) #x
    #define assert(test) ((test) ? (void) 0 \
        _Assert(__FILE__":"__STR__(__LINE__)"" #test)
#endif

/* _Assert function, xassert.c */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

void _Assert(char *msg) 
{
    fputs(msg, stdrr);
    fputs("-- assertion failed\n", msg);
    abort();
}

ctype.h

该头文件中声明了识别和转换字符的函数。实现,转换表?

  • isalnum
  • isalpha
  • iscntrl
  • isdigit
  • isgraph
  • ispunct
  • isspace (‘’ ‘\f’ ‘\t’ ‘\v’ ‘\r’ ‘\n’)
  • isupper
  • isdigit
  • tolower
  • toupper

errno.h

系统调用出错标志变量(平时编程中数学运算溢出)

float.h

定义了浮点数值运算中可以展开为各种范围和参数的宏。eg, FLT_MAX_EXP, DBL_MAX_EXP, FLT_MAX, FLT_MIN, DBL_MAX, DBL_MIN

limits.h

定义了数值的范围,eg. CHAR_MIN, CHAR_MAX, INT_MAX, INT_MIN, SHRT_MIN, SHRT_MAX

locale.h

区域设置

  • setlocale
  • localeconv

math.h

数学库

  • NaN Not a Number, 对负数开方sqrt(-1.0)、对负数求对数(log(-1.0))、0.0/0.0、0.0*inf、inf/inf、inf-inf这些操作都会得到nan。
  • inf
  • sin, cos, tan
  • log, log10, exp, sqrt
  • floor, ceil
  • fabs

setjmp.h

实现在函数间的跳转

  • jmp_buf
  • setjmp
  • longjmp

signal.h

进程间通信信号

  • SIGINT, SIGABRT
  • signal
  • raise

stdarg.h

变长参数的处理。参数传递是通过栈来实现的,一般调用都是从右至左的顺序压参数入栈, 因此参数与参数之间是相邻的,知道前一个参数的类型及地址,根据后一个参数的类型就可以获取后一个参数的内容。 对于变长参数函数,结合一定的条件,我们可以根据最后一个指定参数获取之后的省略参数内容。

那么,要怎么指定上诉的“一定的条件”呢?最简单的方法就像printf等函数一样, 使用格式化占位符。分析格式化字符串参数,通过事先定义好的格式化占位符可知可变参数的类型及个数, 从而获取各个参数内容。一般对于可变参数类型相同的函数也可直接在强制参数中指定可变参数的个数和类型, 这样也能获取各个参数的内容。

  • va_list
  • va_start
  • va_arg
  • va_end
int var_print
//sum为求和函数,其参数类型都为int,但参数个数不定
//第一个参数(强制参数)n指定后面有多少可变参数
int sum(unsigned int n, ...)
{
    int sum=0;
    va_list args;
    va_start(args, n);
    while (n0) {
        //通过va_arg(args,int)依次获取参数的值
        sum + =va_arg(args,int);
        n--;
    }
    va_end(args);
    return sum;
}

stddef.h

提供了实现遍历可变参数的宏

  • ptrdiff_t, size_t, wchar_t, NULL, offsetof

stdio.h

输入输出,文件IO

stdlib.h

定义和声明那些没有明显归属的宏和函数。

  • 整型数学 abs, div, labs, ldiv
  • 算法 bsearch, qsort, rand, srand
  • 文本转换 atof, atoi, atol, strtod, strtol, strtoul
  • 多字节转换 mblen, mbstowcs, mbtowc, wcstombs, wctomb
  • 存储分配 calloc, malloc, realloc, free
  • 环境接口 abort, atexit, exit, getenv, system

string.h

数组和字符串数组操作

  • memcpy, memcmp, memset
  • strcpy, strcat, strlen,
  • strchr, strrchr, strstr
  • strerror
#include <string.h>
static char str[] = "?a???b...#c";
char *t;
t = strtok(str, "?"); /* t points to the token "a" */
t = strtok(NULL, ","); /* t points to the token "??b" */

time.h

时间相关函数

  • struct tm
  • clock
  • struct tm *localtime(const time_t *timer)
  • strftime
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;
};