Thread: I want to know is it usually displayed(Error0)or it is my code which may be defected!

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    28

    I want to know is it usually displayed(Error0)or it is my code which may be defected!

    Here is my code

    Code:
    //Program in C
    /* Using strchr() & strcmp()
    * function(s) to do some conditional(s) */
    
    
    #include <string.h>
    #include <stdio.h>
    #include <conio.h>
    main() {
    clrscr();
    
    
    char name[20];
    char *ptr;
    /* Taking an animal name from user */
    
    
    printf("Hello! Which animal does meow sound? ");
    fgets(name, 20, stdin);
    
    
    if ((ptr = strchr(name, '\n'))) {
        //Replacing newline with NULL
        *ptr = '\0';
    }
    
    
    //Performing check on user input
    char match[4] = "Cat";
    if (strcmp(name, match) == 0) {
        printf("Bravo! You are correct.");
        getch();
    } else {
        perror("Sorry! That was Cat.");
        getch();
    }
    
    
    return(0);
    
    
    }
    Prompt: Hello! Which animal does meow sound?
    Input: Doggy
    Output: Sorry! That was Cat.: Error 0

    Why is Error 0 being displayed?
    Please give me explanation.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Although you haven't bothered to give us any information about what system/compiler/IDE you're using, I assume that you're running your code from an IDE and it's simply telling you the return code of the program. Since you're returning 0 from main, it says Error 0. Try returning 1 from main and see if it says Error 1. (Remember to change it back to 0, though.)
    Explode the sunlight here, gentlemen, and you explode the entire universe. - Plan 9 from Outer Space

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    perror shows value of errno. Since no error actually occurred - errno is 0. This is what is being printed
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by vart View Post
    perror shows value of errno. Since no error actually occurred - errno is 0. This is what is being printed
    I doubt it has anything to do with errno. That would be strange indeed.
    Explode the sunlight here, gentlemen, and you explode the entire universe. - Plan 9 from Outer Space

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by algorism View Post
    I doubt it has anything to do with errno. That would be strange indeed.
    What would be strange?

    Code:
    //Program in C
    /* Using strchr() & strcmp()
    * function(s) to do some conditional(s) */
     
     
    #include <string.h>
    #include <stdio.h>
    
    int main() {
     
     
    char name[20];
    char *ptr;
    /* Taking an animal name from user */
     
     
    printf("Hello! Which animal does meow sound? ");
    fgets(name, 20, stdin);
     
     
    if ((ptr = strchr(name, '\n'))) {
        //Replacing newline with NULL
        *ptr = '\0';
    }
     
     
    //Performing check on user input
    char match[4] = "Cat";
    if (strcmp(name, match) == 0) {
        printf("Bravo! You are correct.");
        getchar();
    } else {
        perror("Sorry! That was Cat.");
        getchar();
    }
     
     
    return(0);
     
     
    }
    This is compiled by gcc 5.4 on Ubuntu and run from terminal without any IDE

    Code:
    $ ./test
    Hello! Which animal does meow sound? as
    Sorry! That was Cat.: Success
    So instead of Error 0 it prints Success
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    I'm blind.
    You're right.
    Last edited by algorism; 08-27-2017 at 12:13 PM.
    Explode the sunlight here, gentlemen, and you explode the entire universe. - Plan 9 from Outer Space

  7. #7
    Registered User
    Join Date
    Aug 2017
    Posts
    28
    Thanks everyone! For helping me out. Actually for me replacing perror() with printf() did the job & also changing main() function's return value from 0 to 1 does the same.

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    I was totally mistaken about the return value of main. It had nothing to do with it. I didn't notice that you were using perror where you should have just used printf.

    You should never use perror unless you have already determined that an error has occurred. Library functions indicate that an error has occurred by a special return value (e.g., fopen returns NULL if an error occurs). strcmp doesn't have an error return value, so it never makes sense to call perror after a strcmp call.

    Here's an example of using perror properly. Notice that we only call it after we know an error has occurred.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        FILE *f = fopen("this_file_does_not_exist", "r");
        if (f == NULL) {
            perror("fopen");
            exit(EXIT_FAILURE);
        }
        printf("The file opened somehow!\n");
        fclose(f);
        return 0;
    }
    Explode the sunlight here, gentlemen, and you explode the entire universe. - Plan 9 from Outer Space

  9. #9
    Registered User
    Join Date
    Aug 2017
    Posts
    28
    Yeah, Thaks man.
    It all makes sense now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displayed errors
    By jperez726 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2010, 12:03 AM
  2. Cannot get text to be displayed
    By Iconate in forum Game Programming
    Replies: 4
    Last Post: 02-18-2010, 12:39 PM
  3. While Loop Query - code displayed
    By darren78 in forum C Programming
    Replies: 13
    Last Post: 02-17-2009, 10:44 AM
  4. seeing what been displayed
    By c_programmer in forum C Programming
    Replies: 3
    Last Post: 12-14-2006, 03:29 PM
  5. Page Cannot be Displayed
    By Padawan in forum Tech Board
    Replies: 5
    Last Post: 07-04-2004, 10:00 AM

Tags for this Thread