Redirected
1,015 views
3 votes
3 votes
Difference between return 0, return 1, return -1, exit in C?

2 Answers

Best answer
6 votes
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 votes
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.

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked 3 days ago
47 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked Apr 10
103 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
252 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how