1

What is the use of return(0) in C? I have written programs which work fine without using it. Please explain it to me in a simplified manner, since I'm a beginner in C. If the answer is complex it might confuse me even more...

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float km, meter, feet, inch, cm;
    printf("Enter the distance(must be in km: \n");
    scanf("%f", &km);
    cm = 100000*km;
    inch = 39370.1*km;
    meter = 1000*km;
    printf("The distance between the cities in cm is %f \n", cm);
    printf("The distance between the cities in inches is %f \n", inch);
    printf("The distance between the cities in meters is %f \n", meter);
}
sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
  • it is simply an exit code – Mox Aug 18 '19 at 09:05
  • @Mox That one is a duplicate of [What should main() return in C and C++](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c)... – Shawn Aug 18 '19 at 09:25

3 Answers3

5

This is to say if the program success or failed .

You can also use return EXIT_SUCCESS; or return EXIT_FAILURE;

You can check the return like this :

enter image description here

Fpasquer
  • 381
  • 3
  • 15
  • But for success (true) I think 1 is used instead of 0 – Tushar Sharma Aug 18 '19 at 09:12
  • Also my programming is running fine without return(0) – Tushar Sharma Aug 18 '19 at 09:12
  • 1
    @TusharSharma Returning 0 (Or calling `exit()` with 0 as the status) indicates successful completion of the program in C. 1 would indicate an error of some kind. – Shawn Aug 18 '19 at 09:14
  • @Shawn ok so you mean here that Boolean logic isn't applicable because according to it 1 is true I am new that's why I'm so much confused in c – Tushar Sharma Aug 18 '19 at 09:16
  • No @shawn. Exit interrupt the program some ASM actions are not done with exit. In this case it's better to use return – Fpasquer Aug 18 '19 at 09:17
  • @TusharSharma 1 should work as an `EXIT_FAILURE` (definitely will work to signal `main`'s failure on Unixes), but as far as concrete values are concerned, the C standard guarantees only that `0` means `EXIT_SUCCESS`. – Petr Skocik Aug 18 '19 at 09:19
  • @TusharSharma: `main()` does not return a boolean, but an `int`. And by common sense returning `0` means "all good", values different from `0` indicate various other cases aside of "all good". – alk Aug 18 '19 at 09:20
  • @TusharSharma It's not boolean logic. It's very common for programs to use different positive exit statuses to indicate what sort of error it was, and for whatever called the program to check that to determine what to do next. `grep`, for example, normally exits with 0 if it matched lines, 1 if it didn't, and 2 on other errors. – Shawn Aug 18 '19 at 09:20
  • @TusharSharma: think of the return value is an error status. It becomes more logical to have `0` for success as it means no error. – chqrlie Aug 18 '19 at 09:42
  • @Fpasquer: `exit` does not “interrupt” the program. Per C 2018 7.22.4.4 2, “The `exit` function causes normal program termination to occur.” Per 5.1.2.2.3 1, “a return from the initial call to the `main` function is equivalent to calling the `exit` function with the alue returned by the `main` function as its argument; reaching the `}` that terminates the `main` function returns a value of 0.” – Eric Postpischil Aug 18 '19 at 11:29
  • 1
    @PSkocik: The meaning of 1 as an exit status is implementation-defined, per C 2018 7.22.4.4 5. – Eric Postpischil Aug 18 '19 at 11:30
  • @EricPostpischil Yes, that's what I was saying: C only guarantees that 0 be interchangeable with EXIT_SUCCESS, not necessarily that 1 should mean failure. – Petr Skocik Aug 18 '19 at 11:35
3

Since the C99 standard, you don't need an explicit return 0; statement at the end of the main function, the compiler will add it for you implicitly.

Also see e.g. this main function reference.

And note that this implicit return 0; is only valid for the main function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

You don't need it in main, because main has been special-cased since c99 to behave as if there was an implicit return 0; before the closing }. (The parentheses as in return(0); are unnecessary.)

As for any other function if it returns an int, and you don't return one, the caller of the function will invoke undefined behavior upon trying to use the return value, which is bad, so if you declare a function to return a value, it should return one, out of consideration for the caller.

In main's case, the caller is (indirectly) the OS, and the return value is used to signal the failure (nonzero return value, or more portably, the value of the EXIT_FAILURE macro) or success (return value of 0 or the EXIT_SUCCESS macro, which is guaranteed to be ==0) of executed programs. The value can be used for example in shell scripts.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142