9.16. perror(), strerror()
函式原型
#include <stdio.h>
#include <string.h> // strerror() 需要
void perror(const char *s);
char *strerror(int errnum);說明
傳回值
範例
參考
Last updated
#include <stdio.h>
#include <string.h> // strerror() 需要
void perror(const char *s);
char *strerror(int errnum);Last updated
int s;
s = socket(PF_INET, SOCK_STREAM, 0);
if (s == -1) { // some error has occurred
// 印出 "socket error: " + 錯誤訊息:
perror("socket error");
}
// 類似的
if (listen(s, 10) == -1) {
// 這會印出 "一個錯誤: " + errno 的錯誤訊息:
printf("an error: %s\n", strerror(errno));
}