0

I only know and understand return 1 and 0. Why was return 2 used in below?

if (candidate_count > MAX)
{
    printf("Maximum number of candidates is %i\n", MAX);
    return 2;
}

In some cases, I also saw return -1. Can someone explain why was it used?

Dion
  • 5
  • 6
  • Without more context it's impossible to day. Maybe that's how the function is supposed to operate? A C function can return whatever it wants so long as it makes sense. Maybe `2` is a particular condition being met. – tadman Jun 04 '20 at 22:19
  • It returns the value `2` to the calling function. – Christian Gibbons Jun 04 '20 at 22:19
  • As a minimum, you need to show the function declaration (the line that has the function name, return type, and the list of parameters). – user3386109 Jun 04 '20 at 22:32
  • Hi and thanks for all the input, it's actually inside MAIN and not inside a created function, sorry about that. I noticed that when the condition is not met, it just stops everything like what would happen when you use return 1. Prior to writing return 2, return 1 was already used in an earlier line. I wonder if it has anything to do with that. – Dion Jun 04 '20 at 22:40
  • `main` normally returns 0 to indicate success. Any other value indicates failure. The return value from `main` is only useful if some other program looks at it. For example, a bash script might look at the value. On the other hand, if you're running the program in a terminal window, the return value from `main` is not used. – user3386109 Jun 04 '20 at 23:20
  • Some programs use different exit status codes to indicate different conditions. For example, `grep` returns `0` when it' successfully matched lines, `1` to indicate that it didn't get any errors but it also didn't match any lines, and some other value to indicate that it got an error (e.g. the file doesn't exist). – Barmar Jun 04 '20 at 23:37
  • I see and it's making sense now. From what I understand, any return value that is != 0, means program is not a success and therefore just returns the function back to the caller. Thanks guys – Dion Jun 04 '20 at 23:52

0 Answers0