![](/img/trans.png)
[英]no return value from one C function written for linux, how does c compiler deal with that?
[英]How does one check the meaning of a numerical return value in C under Linux?
假設我正在調用write(),並且返回錯誤-8。 如何檢查代表的已記錄錯誤返回值之一?
你通常會用
man functionname
也許
man -S2 functionname # specify other section (2, 3, 5, 7)
大多數成功返回0的函數,失敗返回nonnull的函數說明它們也設置了errno
。 您可以獲得errno
全局變量 1的含義
errno.h
strerror
函數( #include <string.h>
) 或使用方便的幫助程序perror
根據strerror
和給定的前綴打印錯誤消息:
perror("oops, popen failed with: ");
1好,從技術上講,它不必是全局變量。 取決於您的操作系統/庫,它可能是一個宏,一個線程局部變量,兩者等等。
查找strerror
聯機幫助頁。 (請注意,盡管write()
僅應在最差的-1
處返回,並在errno
返回錯誤代碼)。
形式上,它不是返回-8,而是錯誤代碼。 這些錯誤代碼可以在手冊中找到(例如: http : //linux.die.net/man/2/write ),並且您應該使用這些代碼來驗證是否一切正常。
因此,您將使用:
write(..);
if( errno == EFAULT ) { ...}
您不應打印出返回值本身,而應打印出strerror(return_value)
。 您也可以查看man errno
以獲取錯誤描述。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.