Call function without parameters and brackets

In the following code fragment, the main function calls the foo function without any parameters and brackets. It is strange that this code can be compiled by gcc. I really check the build code and find out that the compiler is simply ignoring this line. So my question is, in what situation is this type of code used? Or gcc support is just a coincidence, and in fact it is completely useless.

int foo(int a,int b) { return a+b; } int main() { foo; // call foo without parameter and parenthesis return 0; } 

Its assembly code reset by objdump -d

 00000000004004c0 <main>: 4004c0: 55 push %rbp 4004c1: 48 89 e5 mov %rsp,%rbp 4004c4: b8 00 00 00 00 mov $0x0,%eax 4004c9: 5d pop %rbp 4004ca: c3 retq 4004cb: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 
+4
source share
3 answers

This is no different from having any other expression and ignoring its meaning, for example:

 int main(void) { 42; return 0; } 

there is nothing special, it does not call a function, since operator-functions () not used. All you do is "calculate" the address of the functions, and then ignore it.

+9
source

The expression foo is evaluated (indicating the address of the function), and the result is discarded. Without an operator () function is not called.

+6
source

foo not called, it is just passed (and not attached to anything).

+1
source

All Articles