I saw the following code snippet in some C program:
char *x;
if (s == NULL)
return(NULL);
if ((x = strdup(s)))
return x;
What's the purpose of redundant parentheses in return(NULL)?
I saw the following code snippet in some C program:
char *x;
if (s == NULL)
return(NULL);
if ((x = strdup(s)))
return x;
What's the purpose of redundant parentheses in return(NULL)?
Judging from the if
statement below, the author simply likes writing parentheses.
Theoretically, since NULL
is a preprocessor macro you could define it to something that is not enclosed in parens, and therefore might have different behavior depending on which expression it is part of due to operator precedence rules -- in which case, wrapping it in extra parens would be "safer".
Practically, this isn't going to happen. Even contemplating such a scenario makes me feel dirty, so I cannot imagine what would be the fate of someone who actually did this.
Two reasons:
return
as macro when they are debugging such that it does some form of additional logging before returning normally. These people are nuts.Some people like parens on their returns. My only quibble is that the code is inconsistent.
Some people view return
as a function so they put the extra parentheses. Even if return
is actually a statement.
Note that some other people also view sizeof
as a standard library function and like to put extra parentheses when the operand is an expression. Of course sizeof
is an operator and not a function.
There is no specific purpose for them in this case. Usually I prefer putting parenthesis for return statements.
You should either consistently put them or not put them at all.
One possible explanation for the inconsistency here is that NULL is a macro, and some people have a habit of surrounding macros with parenthesis to make sure they are properly contained.