2012-04-22 66 views
0

如何将错误添加到perror堆栈?如何将我的错误的描述添加到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; 
} 
+5

这不是在AAAAAAAAAAAAALLLL如何errno子系统工作... – 2012-04-22 04:05:43

+0

如果这是C++ .. **使用例外!! ** – jli 2012-04-22 04:05:54

+0

不是C++只是c。 – 2012-04-22 04:06:24

回答

2

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

2

唯一的方法是更改​​C标准库,而你不想这样做。

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