用好C语言的中库,系统错误的处理

代码很简单,testc.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <errno.h>                                                           
 4 #include <string.h>
 5 
 6 #define Numerous 0xFFFFFFFFFFFFFF
 7 
 8 int main(int argc, char **argv)
 9 {
10     char *ptrCh = (char *)malloc(sizeof(char) * Numerous);
11     if(ptrCh == NULL){
12         printf("There is an error: %s\n", strerror(errno));
13     }
14 
15     return 0;
16 }

结果是显而易见的

There is an error: Cannot allocate memory

使用函数strerror输出系统的错误,使用errno获取系统的错误的详细信息。

posted @ 2021-08-24 09:01  叕叒双又  阅读(58)  评论(0编辑  收藏  举报