linxu中系统错误码errno

Unix errno值、

  •   只要一个Unix函数(如,某个套接字函数)中有错误发生,全局变量errno就被设置为一个指明该错误类型的正直,函数本身则通过返回-1.
  •   errno的值只在函数发生错误时被设置,如果函数不返回错误,errno的值就没有定义。
  •   errno的所有证书错误值都是常值,具有以“E”开头的全大写字母名字,并通常在<sys/errno.h>头文件中定义,0值不表示任何错误;
  •   sys_errlist
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h> int main(int argc, char **argv) { int sockfd = 0; if ((sockfd = socket(0, SOCK_STREAM, 0)) < 0) { if (errno >= 0 && errno <= sys_nerr) printf("socket created, failed[%d], reason:%s ", errno, sys_errlist[errno]);
     printf("%s ",strerror(errno)); //头文件string.h内 }
else { printf("socket successful "); } return 0; }

输出结果:

socket created, failed[97], reason:Address family not supported by protocol
Address family not supported by protocol
原文地址:https://www.cnblogs.com/weiyouqing/p/13055203.html