C function call without bracket

  • A+
Category:Languages

Here is a simple C program:

#include <stdio.h>  int main(void) {     printf("Display something/n");     fflush stdout;     return 0; } 

Compiled with msys2 mingw-w64 gcc version 7.3.0 and option -Wall and everything works fine, just as if the 5th line was fflush(stdout);.

I tried to reproduce such a call with my own function, but I get the perfectly expected error

src/main.c: In function 'int main(int, char**)': src/main.c:5:18: error: expected ';' before 'parameter'   custom_function parameter;                   ^~~~~~~~~ 

So, what happens with the fflush function? Can somebody explain me? Do you have the same behavior with other C compilers?


Let's see preprocessor output (using MinGW, and gcc -E test.c command line):

   fflush  # 5 "test.c" 3           (&(* _imp___iob)[1]) # 5 "test.c"                 ; 

as you see stdout is a macro which expands to (&(* _imp___iob)[1]) with parentheses.

so the compiler uses those parentheses and the syntax is okay.

But that's only because of macro magic, and the fact that most macros are protected by parentheses to avoid side-effects with other tokens (operator precendence for instance)

You can reproduce that without any includes with this simple code:

#define arg ("hello")  void f(const char *x) { }  int main(int argc, char** argv) {     f arg;     return 0; } 

Of course this is bad practice, confuses IDEs (and humans), so just don't do it.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: