如何将我的错误描述添加到perror函数

时间:2012-04-22 04:03:33

标签: c error-handling errno

如何在perror堆栈中添加错误?

这是我想要的一个例子

#include <stdio.h>
#include <stdlib.h>

int Div (int a, int b, int * c) {
   if (b == 0) {
       // add to perror: "cannot divide by zero!"
       return 0;
   }
   *c = a / b;
   return 1;
}

int main () {
   int n;
   if (!Div(2, 0, &n)) {
      perror("could not divide");
   }
   return 1;
}

2 个答案:

答案 0 :(得分:2)

没有标准(或非标准,在我所知的系统上)添加新errno值的方法;您可以指定errno使用现有值,但对于不属于标准库的任何内容,这不是一个好主意。

答案 1 :(得分:2)

唯一的方法是更改​​C标准库,但您不希望这样做。

如果您更改libc并使用修改后的errno,则可以添加自己的{{1}}号码。但是,您的程序只能在具有修改后的“标准”库的系统上正常工作。