[英]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.