2,140 views
3 3 votes
Difference between return 0, return 1, return -1, exit in C?

2 Answers

Best answer
6 6 votes

Before this question even I thought these statements were true.

Specific to C as it doesn't support throwing exceptions programmers used the return as a way for your routine to send some information to the parent routine that called it which is considered to be an exit status.

There was something of a convention that 0 meant success, a positive number meant minor problems, and a negative number meant some sort of failure.

Actuallly, after digging into a lot of manuals and C documentation I found this : (here's the link, look at line 54 and 55)

In stdlib.h the macros EXIT_SUCCESS and EXIT_FAILURE are defined like this :

#define EXIT_SUCCESS 0 
#define EXIT_FAILURE 1


These 2 macros can be used as the argument to the exit function declared in stdlib.h and they can also be used as the return value of the main function.

Thank you for the question.

sudoankit.

• edited by
2 2 votes
exit() funtion is a system call which terminate whole program while return statement will terminate only function .

if we use exit(0); or return 0; in main function then effect will be same.

return 1 or return -1 simply returning values 1 and -1 respectively.
Position:
Show:

Related questions

1 1 vote
0 0 answers
59
59 views
khanshahima4 asked 1 day ago
59 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...
13 13 votes
1 1 answer
1.2k
1.2k views
GO Classes asked Jul 2
1,202 views
Assume:$\texttt{short = 2 bytes}$, $\texttt{int = 4 bytes}$, $\texttt{char = 1 byte}$$\texttt{int}$ needs $4$-byte alignment and the final structure size is rounded to a ...
16 16 votes
2 2 answers
642
642 views
GO Classes asked Jul 2
642 views
Assume:struct Student { double gpa; }; struct Student alice; struct Student *sptr = &alice;Which of the following correctly assigns $\texttt{4.0}$ to $\texttt{alice.gpa}$...
9 9 votes
1 1 answer
480
480 views
GO Classes asked Jul 2
480 views
Consider the following code idea from the source:typedef struct { double x, y; } Point; void reset(Point p) { p.x = p.y = 0; } int main() { Point a = {12.0, 42.0}; Point ...