errno.h(c标准库)

 

1 /*yvals.h*/
2 #define _YVALS_H_
3 
4 #define MYEDOM  33
5 #define MYERANGE    34
6 #define MYEFPOS 35
7 #define MYERRMAX    36

 

 1 #define _MY_ERRNO_H_
 2  /* 
 3   * 这种宏保护与上面的差别。"yvals.h"会被几个标准头文件包含,
 4   * 它可能在一个预处理中被屡次请求,一旦"yvals.h"变为预处理的一部分,
 5   * 宏保护就会跳过#include预处理指令。这个头文件也就不会被重复地读入
 6   */
 7 #ifndef _YVALS_H_ 
 8 #include "yvals.h"
 9 #endif
10 
11 #define MY_EDOM    MYEDOM     /*域错误,参数值产生的结果没有定义*/
12 #define MY_ERANGE    MYERANGE     /*溢出*/
13 #define MY_EFPOS    MYEFPOS
14 #define MY_NERR    MYERRMAX
15 
16 extern int errno;
17 #endif

 

1 /*my_error.c*/
2 #include "my_errno.h"
3 #undef errno
4 
5 int errno = 0;

 

 1 /*my_t_errno.c*/
 2 #include <assert.h>
 3 #include <math.h>
 4 #include <stdio.h>
 5 #include <errno.h>
 6 #include "my_errno.h"
 7 
 8 int main()
 9 {
10     assert(errno == 0);
11     perror("No error reported as");
12     errno = MY_ERANGE;
13     assert(errno == MY_ERANGE);
14     perror("Range error reported as");
15     errno = 0;
16     assert(errno == 0);
17     sqrt(-1.0);
18     assert(errno == MY_EDOM);
19     perror("Domain error reported as");
20     puts("success testing 'my_errno.h'");
21 
22     return 0;
23 }
相关文章
相关标签/搜索