YouTip LogoYouTip

C Function Perror

# C Library Function - perror() [![Image 3: C Standard Library - ](#) C Standard Library - ](#) ## Description The C library function **void perror(const char *str)** outputs a descriptive error message to the standard error stream (stderr). It first outputs the string **str**, followed by a colon, and then a space. ## Declaration Below is the declaration of the perror() function. void perror(const char *str) ## Parameters * **str** -- This is a C string containing a custom message that will be displayed before the original error message. ## Return Value This function does not return any value. ## Example The following example demonstrates the usage of the perror() function. #include int main (){ FILE *fp; /* First, rename the file */ rename("file.txt", "newfile.txt"); /* Now, let's try to open the same file */ fp = fopen("file.txt", "r"); if( fp == NULL ) { perror("Error: "); return(-1); } fclose(fp); return(0);} Let's compile and run the above program. This will produce the following result because we are trying to open a file that does not exist: Error: : No such file or directory [![Image 4: C Standard Library - ](#) C Standard Library - ](#)
← Cpp LoopsC Function Scanf β†’