2012-04-22 60 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號碼。但是那麼你的程序將只能在你修改的「標準」庫的系統上正常工作。