Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brackets around 0 in "return (0);" statement in 'main' function - what are they for? [duplicate]

Tags:

c++

Usually auto-generated c++ "main" function has at the end

return (0);

or

return (EXIT_SUCCESS);

But why there are parentheses in above statements? Is it related to C language or something?

// EDIT

I know this is correct, but someone put these brackets for a reason. What's the reason?!

like image 783
adf88 Avatar asked Jul 01 '10 07:07

adf88


4 Answers

This is actually a requirement for BSD kernel source file style.

man 9 style says:

Space after keywords (if, while, for, return, switch).

and

Values in return statements should be enclosed in parentheses.

like image 57
greg Avatar answered Nov 17 '22 12:11

greg


They are not required (even in c). I think some people use them to make 'return' look more like a function call, thinking it is more consistent.

Edit: It's likely the generator does this for it's own reasons. It may be safer or easier for it to work this way.

like image 10
sje397 Avatar answered Nov 17 '22 12:11

sje397


But why there are parentheses in above statements? Is it related to C language or something?

No. As far as I can tell, C has never required parentheses for return statements. This appears to have been the case even before the first ANSI C standard.

This is actually a very interesting question, however, as I've seen that style prevalent among certain C programmers.

I think the most likely guess as to why this style came about is because all other branching statements (for, while, if, switch) require parentheses around expressions. People might have been unaware that they could omit the parentheses for return statements or were aware of this but wanted to achieve a more uniform look to their code.

The ternary ?: operator is sort of an exception as it is an operator and doesn't require parentheses around the conditional expression, yet people often write parentheses there as well regardless of whether it's needed. Some might find it serves to 'group' an expression into a single unit visually.

My second best guess is that this style was influenced by other languages popular at the time. However, popular, procedural alternatives at the time like Pascal did not require that syntax either (Pascal did not even have return values in the C sense but only output parameters) so if this is the case, I'm not aware of any particularly language from which this style originated.

[Subjective] I prefer styles that require the least amount of superfluous decoration to the code whether it comes to naming conventions or how to format it or whether to use additional parentheses where unnecessary. I find that any such decoration tends to be a matter of unique personal preference and falling in love with one way of decorating code just means that someday you'll have to deal with a completely different way (unless you work strictly alone, in which case I envy you). [/Subjective]

like image 9
stinky472 Avatar answered Nov 17 '22 11:11

stinky472


As any valid expression can be passed to return, those brackets can be added if desired. It is like doing this:

int i = (0);

You can nest an expression in as many brackets as you like:

return (((((0)))));
like image 6
Janick Bernet Avatar answered Nov 17 '22 10:11

Janick Bernet