The strerror () function in C

The function char * strerror (int errnum) searches for an internal array for errnum error numbers and returns a pointer to a string of error messages.

The function char * strerror (int errnum) searches for an internal array for errnum error numbers and returns a pointer to a string of error messages.

Declaring strerror () function in C

Here is the declaration for strerror () in C:

 char * strerror ( int errnum ) 

Parameters

errnum - Usually errno , describes the error number.

Returns the value

This function returns a pointer to the error string describing errnum error.

For example

The following C program illustrates the usage of strerror () in C:

 #include #include #include int main () { FILE * fp ; fp = fopen ( "thuvienc.txt" , "r" ); if ( fp == NULL ) { printf ( "Error: %sn" , strerror ( errno )); } return ( 0 ); } 

Compiling the C program will result (because we open a file that does not exist):

The strerror () function in C Picture 1The strerror () function in C Picture 1

According to Tutorialspoint

Previous article: strcspn () function in C

Next lesson: strlen () function in C

5 ★ | 1 Vote