Extra brackets with function names in C/C++

Consider below C program. The program has extra bracket around function name.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to show that extra brackets with function
// name work
#include <stdio.h>
  
void (foo)(int n)
{
   printf("Function : %d ", n);
}
  
int main()
{
   (foo)(4);
   return 0;  
}

chevron_right


Output:

Function 4

So putting extra bracket with function name works in C/C++.



What can be use of it?
One use could be, if we have a macro with same name as function, then extra brackets avoid macro expansion wherever we want the function to be called.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to show that extra brackets with function
// name can be useful if we have a macro with same name
#include <stdio.h>
#define foo(n)  printf("\nMacro : %d ", n);
  
void (foo)(int n)
{
   printf("Function : %d ", n);
}
  
int main()
{
   (foo)(4);
   foo(4);
   return 0;
}

chevron_right


Output:

Function 4
Macro : 4

If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.