2

I shown some code that i did understand.

following code is example code.

static void (_func)(int p);

int main()
{
....
    _func(3);
....
}

static void (_func)(int p)
{
 ....
}

Generally I know that function surrounded with parentheses is used with '*' for function pointer as (*_func), but above code why did surround the function with parentheses at declaration of function?

Is there some reason to use this method ?

  • 1
    I understand that English is not your first language, but you should consider learning the correct words. These symbols: `( )` are called "parenthesis". – Jonathon Reinhart Jun 20 '13 at 06:21
  • Thanks Jonathon Reinhart for letting me know correct word related to parentheses. – user2503838 Jun 20 '13 at 06:26
  • 1
    "parentheses" for more than one () and "parenthesis" for only one () – 0decimal0 Jun 20 '13 at 06:29
  • Possible duplicate of [What do the parentheses around a function name mean?](https://stackoverflow.com/questions/13600790/what-do-the-parentheses-around-a-function-name-mean) – ShadowRanger Oct 22 '19 at 18:56

1 Answers1

3

Putting parens around a function name will prevent it from being 'overridden' by a function-like macro with the same name.

For example, sometimes a function might be implemented as a macro but it might also need to be implemented as an actual function (one reason might be so that a pointer to it can be obtained). The implementer of this API might put the declaration of the function name and the actual function implementation with the name wrapped in parens so that there's no conflict with the macro name.

Then the user of the API can decide that if for whatever reason they want to use the actual function instead of the macro, they can #undef _func or use the function name wrapped in parens to avoid using the macro.

As mentioned in C99 7.1.4 "Use of library functions":

Any function declared in a header may be additionally implemented as a function-like macro defined in the header, so if a library function is declared explicitly when its header is included, one of the techniques shown below can be used to ensure the declaration is not affected by such a macro. Any macro definition of a function can be suppressed locally by enclosing the name of the function in parentheses, because the name is then not followed by the left parenthesis that indicates expansion of a macro function name. For the same syntactic reason, it is permitted to take the address of a library function even if it is also defined as a macro. The use of #undef to remove any macro definition will also ensure that an actual function is referred to.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760