997 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
SSR17 asked Feb 29
210 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
2 votes
2 votes
1 answer
4
rupamsardar asked Aug 30, 2023
468 views
#include <stdio.h int f(int x) { if(x%2==0) { return f(f(x-1)); } else return (x++); } int main() { printf("%d",f(12)); ret...