doubt in return

I have a doubt pertaining to the keyword 'return'. The doubt is as follows:

(i) What is the difference between return 0; and return (0); ?

(ii)Can we end the execution using return 1; or return 2; instead of using return 0;
Is there any difference between the two?

Kindly clarify my doubts.
I dont believe there is a differnce between 0 and (0), but i'm not sure.

When a program is executed correctly, it returns the value 0. Then the user or the program interacting with this program knows that there hasn't been problems while executing the program.
When the program returns another value as 0, eg 1 or 2, it means someting unexpected happend. The computer keeps doing what he needs to do, so you could use it without see anny effect of it.
1. Extra typing (two parentheses), otherwise absolutely nothing.
2. Every process returns a return code to its parent. The value that is returned
to the parent is the return value from main. main() by standard returns int
which means you can return any integer value you want (returning 0 typically
means success and non-zero is some application-specific error code)
although I believe some OSes only handle 8-bit return codes, so you should
probably only return values 0-255.

(ii) no difference however you can for instance write a console program in windows and use the return value in a batch file:

1
2
3
4
5
6
7
C:>type mybatch.cmd
myprogram
if errorlevel 1 goto failed
goto end
:failed
echo didn't work
:end 


so that is one way that exit code can be used
thanks to all the repliers especially jsmith.
The thing that i have understood is that when a parentheses is used , the value inside the parentheses is to be returned to the calling statement .
But if a parentheses is not used then the code can be interpreted as , the program has been executed without returning any errors .
hence a proper way of writing is return 0; instead of writing 1 or 2 in place of 0.
i hope i have understood it correctly.
Last edited on
Part of your statement does not sound right to me.

The "return" keyword is used to return a value from a function back to the calling statement. The syntax "return expr;" tells the compiler to evaluate the mathematical expression denoted by "expr" and return that result back to the caller. The syntax "return ( expr );" does the exact same thing.

Parentheses around expressions in C/C++ are used only to clarify/denote precedence of operations. eg, the expression 2 + 3 * 4 yields 14 since multiplication has higher precedence than addition, but the expression ( 2 + 3 ) * 4 yields 20 since the parentheses tell the compiler that it should be the sum of 2 and 3 that should be multiplied by 4 (hence the compiler must generate code to add 2 and 3 before multiplying by 4).
thanks jsmith once again.
Topic archived. No new replies allowed.