C being a system programming language allows you handle error at lower levels or system levels. Most functions return -1 or NULL on error and set errno with an error code which is a global variable defined in #include<errno.h>.
As a good C programmer, we can always check errors and take appropriate actions as and when required. As a good practice, developer should set errno to 0 at the time of initialization of the program. A value of 0 indicates that there is no error in the program.
There are two functions defined in C to get error messages associated with errno .
1. perror( string ): perror return text message containing string passed to it as an argument followed by colon followed by textual message associated with an errno value.
2. strerror(int errnum ): It returns pointer to textual represent of current errno value.
#include<stdio.h>
#include<string.h>
#include<errno.h>
int main(int argc,char **argv)
{
fprintf(stderr,"EACCES:%s\n",strerror(EACCES));
errno=ENOENT;
perror(argv[0]);
return 0;
}
As a good C programmer, we can always check errors and take appropriate actions as and when required. As a good practice, developer should set errno to 0 at the time of initialization of the program. A value of 0 indicates that there is no error in the program.
There are two functions defined in C to get error messages associated with errno .
1. perror( string ): perror return text message containing string passed to it as an argument followed by colon followed by textual message associated with an errno value.
2. strerror(int errnum ): It returns pointer to textual represent of current errno value.
#include<stdio.h>
#include<string.h>
#include<errno.h>
int main(int argc,char **argv)
{
fprintf(stderr,"EACCES:%s\n",strerror(EACCES));
errno=ENOENT;
perror(argv[0]);
return 0;
}





0 comments