Skip to content
On this page

perror()


标签:linux/api  

函数简介

打印 errno 对应的错误信息。

c
#include <stdio.h>
void perror(const char *s);
#include <errno.h>
const char *sys_errlist[];
int sys_nerr;
int errno;

函数会在输入的字符串的后面带上错误的信息。

类似的函数还有

使用示例

c
int main()
{
  FILE *fp;

  fp = fopen("tmp", "r");
  if(fp == NULL)
  {
    perror("fopen()");
    exit(1);
  }
  puts("OK!");
  exit(0);
}

如果文件 tmp 不存在,会输出:

bash
fopen(): No such file or directory

冒号及后面的内容是函数为我们带上的错误信息。

Last updated: