C标准库C99头文件

  • <assert.h>
  • <complex.h>
  • <ctype.h>
  • <errno.h>
  • <fenv.h>
  • <float.h>
  • <inttypes.h>
  • <iso646.h>
  • <limits.h>
  • <locale.h>
  • <math.h>
  • <setjmp.h>
  • <signal.h>
  • <stdalign.h>
  • <stdarg.h>
  • <stdatomic.h>
  • <stdbit.h>
  • <stdbool.h>
  • <stdckdint.h>
  • <stddef.h>
  • <stdint.h>
  • <stdio.h>
  • <stdlib.h>
  • <stdnoreturn.h>
  • <string.h>
  • <tgmath.h>
  • <threads.h>
  • <time.h>
  • <uchar.h>
  • <wchar.h>
  • <wctype.h>

本文章是持续更新,对于C99中的头文件进行分析解读和示例。

<assert.h>

Conditionally compiled macro that compares its argument to zero.
通过查看源代码,其中的主要函数如下:

__BEGIN_DECLS/* This prints an "Assertion failed" message and aborts.  */
extern void __assert_fail (const char *__assertion, const char *__file,unsigned int __line, const char *__function)__THROW __attribute__ ((__noreturn__));/* Likewise, but prints the error text for ERRNUM.  */
extern void __assert_perror_fail (int __errnum, const char *__file,unsigned int __line, const char *__function)__THROW __attribute__ ((__noreturn__));/* The following is not at all used here but needed for standardcompliance.  */
extern void __assert (const char *__assertion, const char *__file, int __line)__THROW __attribute__ ((__noreturn__));__END_DECLS#define assert(expr)                            \((void) sizeof ((expr) ? 1 : 0), __extension__ ({          \if (expr)                              \; /* empty */                          \else                               \__assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION);  \}))
#endif

通常使用断言在程序中对表达式的真假做判断。示例代码如下:

int n = 1;
assert( n > 1);

运行的结果是:

test: src/pmscore.c:8: welcomeMessage: Assertion `n > 1' failed.

<complex.h>

(C99) Complex number arithmetic
这个不用多说,数学上的复数处理和计算,通常情况下使用不到。这里的注释挺有趣,since ISO C99 is out hopefully,直白的说没希望了。

/* We might need to add support for more compilers here.  But since ISOC99 is out hopefully all maintained compilers will soon provide the datatypes `float complex' and `double complex'.  */
#if __GNUC_PREREQ (2, 7) && !__GNUC_PREREQ (2, 97)
# define _Complex __complex__
#endif#define complex       _Complex/* Narrowest imaginary unit.  This depends on the floating-pointevaluation method.XXX This probably has to go into a gcc related file.  */
#define _Complex_I  (__extension__ 1.0iF)/* Another more descriptive name is `I'.XXX Once we have the imaginary support switch this to _Imaginary_I.  */
#undef I
#define I _Complex_I#if defined __USE_ISOC11 && __GNUC_PREREQ (4, 7)
/* Macros to expand into expression of specified complex type.  */
# define CMPLX(x, y) __builtin_complex ((double) (x), (double) (y))
# define CMPLXF(x, y) __builtin_complex ((float) (x), (float) (y))
# define CMPLXL(x, y) __builtin_complex ((long double) (x), (long double) (y))
#endif

通常使用复数的方法,示例代码如下:

float _Complex f = -1.0f;
f = csqrtf(f) + 1.0iF;
printf("The complex number is: %g+i%g\n", crealf(f), cimagf(f));double _Complex d = cexp(2.0i * M_PI);
printf("e^(i2PI) = %f+i%f\n", creal(d), cimag(d));

编译示例代码的时候,编译器会提示:/usr/bin/ld: xxx: undefined reference to csqrtf'。解决办法是在编译时加上-lm。原因是gcc指定的默认库文件中,不包括math库。这个确实有点坑,编译时需要手工指定。

gcc -o test -I. -L. -lm -lxxx src/test.c

<ctype.h>

Functions to determine the type contained in character data.
这个不用多说,判断字符类型。基本是字节位移,或利用“与”操作的技巧。有个有趣的地方,是用宏定义对于大小头机的判断,十分简洁。

#define  __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN    4321
#define __PDP_ENDIAN    3412

如果是__BYTE_ORDER == __BIG_ENDIAN,大头机;如果是__BYTE_ORDER == __LITTLE_ENDIAN,小头机。有兴趣的读者可以思考一下,1234真的是挺有用的。:)

<errno.h>

Macros reporting error conditions
没有什么特别的,就是打印错误信息。

<fenv.h>

(C99) Floating-point environment
浮点环境相关设置。

<float.h>

Limits of floating-point types.
一大堆宏定义,主要是对浮点类型的约束。从头文件上面的说明来看,对ISO C标准规定的特性进行了实现。
代码原文件的第24到26行。

/** ISO C Standard:  5.2.4.2.2  Characteristics of floating types <float.h>*/

<inttypes.h>

(C99) Format conversion of integer types
这个文件也是中规中矩的,一大堆关于格式化输出标记的宏定义,用于在 print 时进行格式化。下面截取一段用于示例。有d,有i,有o,是不是很熟悉?不过,特别提出一个小插曲,关于#include_next预编译指令。通常情况下较少遇到,存在于stdint.h头文件里。这个不是标准C的内容,是GNU的扩展,也就是说,在非GNU的编译环境下,是不被编译器识别的。这个预编译指令指示编译器寻找下一个和包含路径排在前面的头文件同名的头文件。

/**  ISO C99: 7.8 Format conversion of integer types <inttypes.h>*/
/* Decimal notation.  */
# define PRId8      "d"
# define PRId16     "d"
# define PRId32     "d"
# define PRId64     __PRI64_PREFIX "d"# define PRIi8      "i"
# define PRIi16     "i"
# define PRIi32     "i"
# define PRIi64     __PRI64_PREFIX "i"/* Octal notation.  */
# define PRIo8      "o"
# define PRIo16     "o"
# define PRIo32     "o"
# define PRIo64     __PRI64_PREFIX "o"

再有就是一些常用的函数,比如:计算绝对值,除法计算,字符串按base转换等。截了前面二个函数声明作为示例。

/* Compute absolute value of N.  */
extern intmax_t imaxabs (intmax_t __n) __THROW __attribute__ ((__const__));/* Return the `imaxdiv_t' representation of the value of NUMER over DENOM. */
extern imaxdiv_t imaxdiv (intmax_t __numer, intmax_t __denom)__THROW __attribute__ ((__const__));

调用结果如下:

intmax_t a = -600;
intmax_t b = 1200;
printf("%d\n", imaxabs(a));
printf("%d\n", imaxabs(b));
printf("%d\n", imaxdiv(b, a));

输出如下:

$ ./test
600
1200
-2

<iso646.h>

全部文件一共46行(Copyright声明什么的就不截取了,所以这个下面只有22行),内容不多,就全部贴在下面了,功能不用多说了,一看就能明白这些宏定义的作用。不过,我本人还是喜欢直接用&&,而不太常用and宏。小声的问一下,这些宏定义的作用是助记符么 :)

/** ISO C Standard:  7.9  Alternative spellings  <iso646.h>*/#ifndef _ISO646_H
#define _ISO646_H#ifndef __cplusplus
#define and &&
#define and_eq  &=
#define bitand  &
#define bitor   |
#define compl   ~
#define not !
#define not_eq  !=
#define or  ||
#define or_eq   |=
#define xor ^
#define xor_eq  ^=
#endif#endif

<limits.h>

Ranges of integer types
int型的上下限范围定义都在这里了。不过,这个是个“假”文件,真正的头文件在“后面”。
什么,头文件还有假的?是的,证据如下。:)

#ifdef _GCC_NEXT_LIMITS_H
#include_next <limits.h>      /* recurse down to the real one */
#endif

本机验证了一下,一共找到4个。

# find . -name "limits.h"
./usr/lib/gcc/x86_64-redhat-linux/10/include/limits.h
./usr/include/linux/limits.h
./usr/include/c++/10/tr1/limits.h
./usr/include/limits.h

哈哈,第一个显然就是那个“假”的头文件了。果断打开最后这个,也许就是real one的那个。截取一段,雅俗共赏一下。硬气的说有没有,我们没有#include_next

/* We only protect from multiple inclusion here, because all the other#include's protect themselves, and in GCC 2 we may #include_next throughmultiple copies of this file before we get to GCC's.  */
# ifndef _LIMITS_H
#  define _LIMITS_H     1#include <bits/wordsize.h>/* We don't have #include_next.Define ANSI <limits.h> for standard 32-bit words.  *//* These assume 8-bit `char's, 16-bit `short int's,and 32-bit `int's and `long int's.  *//* Number of bits in a `char'.  */
#  define CHAR_BIT      8/* Minimum and maximum values a `signed char' can hold.  */
#  define SCHAR_MIN     (-128)
#  define SCHAR_MAX     127/* Maximum value an `unsigned char' can hold.  (Minimum is 0.)  */
#  define UCHAR_MAX     255/* Minimum and maximum values a `char' can hold.  */
#  ifdef __CHAR_UNSIGNED__
#   define CHAR_MIN     0
#   define CHAR_MAX     UCHAR_MAX
#  else
#   define CHAR_MIN     SCHAR_MIN
#   define CHAR_MAX     SCHAR_MAX
#  endif/* Minimum and maximum values a `signed short int' can hold.  */
#  define SHRT_MIN      (-32768)
#  define SHRT_MAX      32767/* Maximum value an `unsigned short int' can hold.  (Minimum is 0.)  */
#  define USHRT_MAX     65535/* Minimum and maximum values a `signed int' can hold.  */
#  define INT_MIN       (-INT_MAX - 1)
#  define INT_MAX       2147483647/* Maximum value an `unsigned int' can hold.  (Minimum is 0.)  */
#  define UINT_MAX      4294967295U/* Minimum and maximum values a `signed long int' can hold.  */
#  if __WORDSIZE == 64
#   define LONG_MAX     9223372036854775807L
#  else
#   define LONG_MAX     2147483647L
#  endif
#  define LONG_MIN      (-LONG_MAX - 1L)/* Maximum value an `unsigned long int' can hold.  (Minimum is 0.)  */
#  if __WORDSIZE == 64
#   define ULONG_MAX    18446744073709551615UL
#  else
#   define ULONG_MAX    4294967295UL
#  endif

看到对于LONG_MAX的宏定义,深感作者的认真,这真的是经过认真计算出来的 18446744073709551615UL。

<locale.h>

Localization utilities
本地化(或者叫作区域)的一些函数功能调用。如setlocale, localeconv,本地化信息的一些结构体struct lconv

/* Structure giving information about numeric and monetary notation.  */
struct lconv
{/* Numeric (non-monetary) information.  */char *decimal_point;     /* Decimal point character.  */char *thousands_sep;     /* Thousands separator.  *//* Each element is the number of digits in each group;elements with higher indices are farther left.An element with value CHAR_MAX means that no further grouping is done.An element with value 0 means that the previous element is usedfor all groups farther left.  */char *grouping;/* Monetary information.  *//* First three chars are a currency symbol from ISO 4217.Fourth char is the separator.  Fifth char is '\0'.  */char *int_curr_symbol;char *currency_symbol;    /* Local currency symbol.  */char *mon_decimal_point;   /* Decimal point character.  */char *mon_thousands_sep; /* Thousands separator.  */char *mon_grouping;      /* Like `grouping' element (above).  */char *positive_sign;       /* Sign for positive values.  */char *negative_sign;        /* Sign for negative values.  */char int_frac_digits;       /* Int'l fractional digits.  */char frac_digits;       /* Local fractional digits.  *//* 1 if currency_symbol precedes a positive value, 0 if succeeds.  */char p_cs_precedes;/* 1 iff a space separates currency_symbol from a positive value.  */char p_sep_by_space;/* 1 if currency_symbol precedes a negative value, 0 if succeeds.  */char n_cs_precedes;/* 1 iff a space separates currency_symbol from a negative value.  */char n_sep_by_space;/* Positive and negative sign positions:0 Parentheses surround the quantity and currency_symbol.1 The sign string precedes the quantity and currency_symbol.2 The sign string follows the quantity and currency_symbol.3 The sign string immediately precedes the currency_symbol.4 The sign string immediately follows the currency_symbol.  */char p_sign_posn;char n_sign_posn;
#ifdef __USE_ISOC99/* 1 if int_curr_symbol precedes a positive value, 0 if succeeds.  */char int_p_cs_precedes;/* 1 iff a space separates int_curr_symbol from a positive value.  */char int_p_sep_by_space;/* 1 if int_curr_symbol precedes a negative value, 0 if succeeds.  */char int_n_cs_precedes;/* 1 iff a space separates int_curr_symbol from a negative value.  */char int_n_sep_by_space;/* Positive and negative sign positions:0 Parentheses surround the quantity and int_curr_symbol.1 The sign string precedes the quantity and int_curr_symbol.2 The sign string follows the quantity and int_curr_symbol.3 The sign string immediately precedes the int_curr_symbol.4 The sign string immediately follows the int_curr_symbol.  */char int_p_sign_posn;char int_n_sign_posn;
#elsechar __int_p_cs_precedes;char __int_p_sep_by_space;char __int_n_cs_precedes;char __int_n_sep_by_space;char __int_p_sign_posn;char __int_n_sign_posn;
#endif
};

<math.h>

Common mathematics functions

<setjmp.h>

Nonlocal jumps

<signal.h>

Signal handling

<stdalign.h>

(C11) alignas and alignof convenience macros

<stdarg.h>

Variable arguments

<stdatomic.h>

(C11) Atomic operations

<stdbit.h>

(C23) Macros to work with the byte and bit representations of types

<stdbool.h>

(C99) Macros for boolean type

<stdckdint.h>

(C23) macros for performing checked integer arithmetic

<stddef.h>

Common macro definitions

<stdint.h>

(C99) Fixed-width integer types

<stdio.h>

Input/output

<stdlib.h>

General utilities: memory management, program utilities, string conversions, random numbers, algorithms

<stdnoreturn.h>

(C11) noreturn convenience macro

<string.h>

String handling

<tgmath.h>

(C99) Type-generic math (macros wrapping math.h and complex.h)

<threads.h>

(C11) Thread library

<time.h>

Time/date utilities

<uchar.h>

(C11) UTF-16 and UTF-32 character utilities

<wchar.h>

(C95) Extended multibyte and wide character utilities

<wctype.h>

(C95) Functions to determine the type contained in wide character data

C99:C标准库接口的头文件集和功能定义参考相关推荐

  1. c51语言的标准库函的头文件,C51编程中头文件的使用

    头文件在C51的编程中是不可缺少的部分.本文将对keil C中常用头文件予以说明,并就如何编写头文件进行初步介绍. 一.C51常见本征函数库 一些常见的头文件都是keil C自带的,在安装目录下的C5 ...

  2. pythondifflib详解_用python标准库difflib比较两份文件的异同详解

    [需求背景] 有时候我们要对比两份配置文件是不是一样,或者比较两个文本是否异样,可以使用linux命令行工具diff a_file b_file,但是输出的结果读起来不是很友好.这时候使用python ...

  3. 用#ifndef、#define、#endif避免头文件的重定义

    在一个大型项目里面,可能会有多个文件同时包含一个头文件,当这些文件编译链接成一个可执行文件时,就会出现大量"重定义"的错误.在头文件中使用#ifndef.#define.#endi ...

  4. gsoap 学习 1-自己定义接口生成头文件

    接口头文件的格式在向导中没有看到明确的说明性的内容,但通过看开发包中示例程序中头文件定义和通过wsdl生成的头文件的内容,可以发现,头文件中都会出现以下几行信息  //gsoap ns service ...

  5. Python标准库03 路径与文件 (os.path包, glob包)

    摘要:Python标准库 文件系统 os.path glob.glob os.path包 os.path包主要是处理路径字符串,比如说'/home/vamei/doc/file.txt',提取出有用信 ...

  6. linux头文件 库,Linux操作系统的头文件和库文件搜索路径

    一. 头文件 1 ""中的头文件,在源文件当前目录查找 2 -I 中指定目录 -I可以在CFLAG中指定 3 gcc的环境变量 C_INCLUDE_PATH, CPLUS_INCL ...

  7. 简洁明了——STL容器库之set头文件常用函数集合

    简介: 1.同vector封装数组,list封装链表一样,set和map封装了二叉树(红黑树,性能优于平衡二叉树) 2.有序且去重 3.插入.删除.查找效率高(二叉树) 4.插入or删除后迭代器仍有效 ...

  8. 标准库:csv --- CSV 文件读写

    CSV (Comma Separated Values) 格式是电子表格和数据库中最常见的输入.输出文件格式. csv 模块实现了 CSV 格式表单数据的读写.其提供了诸如"以兼容 Exce ...

  9. esp32 添加官方组件库中的头文件提示“No such file or directory”【已解决】

    环境 windows下vscode ssh到Linux对esp32项目进行操作. 背景 使用espadf框架下的wwe例程添加#include "baidu_access_token.h&q ...

最新文章

  1. UIView编程体验(一)
  2. 转:Ubuntu下下载工具安装--uget+aria2
  3. python虚拟环境 pyenv_Python 虚拟环境 pyenv、venv(pyvenv)、virtualenv之间的区别
  4. python爬取2019年计算机就业_2019年最新Python爬取腾讯招聘网信息代码解析
  5. Nginx的端口修改问题
  6. java中的垃圾收集器_Java中的垃圾收集
  7. 订阅号获取openid_小程序订阅消息
  8. 淘宝Fourinone介绍及与Hadoop的性能PK
  9. cdn的费是多少_CDN多少钱_CDN服务如何收费_CDN服务价格-华为云
  10. 关于uniapp cheneckbox复选框不显示对号的问题
  11. 个推技术 | ETL工程师必看!超实用的任务优化与断点执行方案
  12. python排名差怎么改,2017中国大学排名爬虫代码修改
  13. 西门子S7-1200PLC和KTP700触摸屏通过USS协议控制MM420变频器
  14. 【VUE实战问题记录】Vue 父组件调用子组件的使用方法
  15. eclipse如何用Debug调试程序
  16. Oracle 查询:一小时内、一天内、一周内等
  17. 开源:Taurus.MVC 框架
  18. 计算机基础知识(2)
  19. docker部署excalidraw画图工具
  20. 2021年高考成绩查询梧州市,2021年梧州高考状元名单公布 今年梧州高考状元是谁资料和分数...

热门文章

  1. 使用keras.applications和keras_applications构建keras.Model
  2. Anaconda点开 一直停留在 loading applications。。。
  3. 在线教育系统源码讲解与代码分析
  4. 论文笔记之SAC提升算法
  5. Linux搭建TFTP服务
  6. 培养青少年学习少儿编程
  7. rxjs 经典使用场景
  8. MATLAB 2014a将m文件打包成jar包
  9. 2021年MathorCupA题
  10. FlyMCU烧入成功,板子无反应