【由浅入深C系列一】C标准库(C99 )接口的头文件集和功能定义参考

本文主要介绍了C语言标准库中的多个头文件,如<assert.h>用于表达式真假判断,<complex.h>处理复数计算,<ctype.h>判断字符类型等。还提及部分头文件在C99、C11、C23等标准中的特性,以及编译时可能遇到的问题和解决办法。


本文章是持续更新,对于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 standard
   compliance.  */
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 ISO
   C99 is out hopefully all maintained compilers will soon provide the data
   types `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-point
   evaluation 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

招财猫_Martin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值