#include<stdio.h>
#define POOLNAME_FMT "Hello"
void main() {
printf((POOLNAME_FMT "Cannot allocate %d bytes" POOLNAME_FMT "in pool not enough memory",5));
}
Why does it give segmentation fault when I use double brackets with printf
. i.e. printf(( ));
?
Because
(a, b)
is actually a single value. It calculatesa
andb
and returns the value ofb
.So what you are doing is basically:
Which is clearly wrong.
When you write
func(a, b)
as a function call, C knows to senda
andb
as separate arguments tofunc
. When you sayfunc((a, b))
, you are explicitely saying(a, b)
is one value and that the result (i.e., the value ofb
) should be sent tofunc
as a single argument.If you compile with warnings, and if your compiler is nice to you, it could warn you about this. If your compiler is not nice, it should still complain that you are giving an
int
where aconst char *
is expected.If using
gcc
, I highly recommend compiling with-Wall
, always.