Thread: Returning found from main

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question Returning found from main

    Good afternoon. what's the meaning of returning non-zero from main?

    Code:
     
    
    /* strindex: return index of t in s, -1 if none */ 
     
    #include <stdio.h> 
    
    #include "Headers/strings.h"
    #include "Strings/strindex.c"
    #include "Strings/getline.c"
    
    #ifndef MAXLINE 
      #define MAXLINE 1000
    #endif  
    
    static const char pattern[] = "ould";
    int found;
    
    int main(void) 
    { 
      char line[MAXLINE];
      
      while(getLine(line,MAXLINE) > 0)
       if(strindex(line, pattern) >= 0)
         printf("%s", line); 
       found++;  
      
      return found;    
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It's a status code. The convention is that returning 0 means success, and non-zero means there was an error. It's up to you which number to use to represent different errors (you'll often see a program's documentation tell you how to interpret the different status codes). This is useful if somebody wants to call your program as part of a script - then they can check if your program succeeded and decide whether or not to proceed.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Imagine a situation where many programs run in one system.These programs must notify the system that everything went good, thus they terminated normally.So they return , by "agreement" , zero to denote success . Otherwise a non-zero value is returned, such as -1.
    In the standard library , stdlib.h we have these macros defined
    Code:
    Macros
    EXIT_FAILURE
    Failure termination code (macro )
    EXIT_SUCCESS
    Success termination code (macro )
    but i personally never have used them until now.

    A comment about your code.It is not a good practice not to initialize variables, even though there are automatically initialized (here it is a global variable) . It makes your code easier to read

  4. #4
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    hum but in that case, I found the pattern and I'm returning a non-zero to point success. Therefore, I didn't understand why found would be failure.

    edit:

    zero to denote success . Otherwise a non-zero value is returned, such as -1.
    so, found is indicating that I didn't find the pattern? hum, then I think I should change the name of the var to wasnotfound, right?
    Last edited by thames; 11-16-2012 at 01:50 PM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Success or failure, or any value, only means to a computer what someone tells it to mean. If you have no control over the system that calls your program, then you must provide it with the expected value for the given conditions. Just because you want to define a non-zero return value from "main()" to be success, doesn't mean the calling program will interpret it that way.

  6. #6
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    doesn't mean the calling program will interpret it that way.
    do you mean every non-zero value returned by main will always be interpreted as failure?

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    not necessarily it depends if it was returned by you or by the system but all non zero values returned by the system are always errors....

  8. #8
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    I see. So found, actually, is really a variable indicating error. Very ambiguous name :\ many thanks.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Sigh!

    From the code, found is computed as the number of times a pattern is matched in input. By returning its value from main(), the calling environment (for example, a shell script, a batch file, even another spawning program) can access that value.


    Conventionally, as specified in the C standard, the return value is conceptualised as an error condition, where zero indicates success and non-zero values indicate other status values. However, that is only a convention. It does not mean a particular C program or the environment in which it runs is required to use it as an error status. It can - as in the code provided here - be a count of some form.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    hum I see.

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    In the posted code, found is always incremented one time, so the program returns 1 every time. You would need braces if you want to put found++ inside the loop or inside the body of the if statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2012, 06:41 AM
  2. Returning to main.
    By Deltahawk12 in forum C++ Programming
    Replies: 11
    Last Post: 09-10-2005, 09:11 PM
  3. Returning to main menu
    By Crash1322 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2003, 05:27 PM
  4. Returning arrays from function to main
    By musayume in forum C Programming
    Replies: 3
    Last Post: 05-12-2003, 04:33 AM
  5. Returning a Tree node back to void main()
    By gazza in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2002, 02:48 AM

Tags for this Thread