What this C statement does?
i=!({ printf("%d\n",r); });
i and r are integers.
I'm trying to parse it using pycparser which doesn't recognize it and raises an error:
pycparser.plyparser.ParseError: :7:6: before: {
Thanks
It looks like it is using a GNU C extension that allows to write a block statement where an expression is expected. The value of the block statement is the value of the last expression of the block.
For example:
int x = ({ int a = 1; a+2; });
will initialize x
with 3
.
In your particular case the extension does not look very useful, because:
i=!({ printf("%d\n",r); });
is identical to:
i=!printf("%d\n",r);
I'm guessing that your original code is probably generated by some dark magic macro.
BTW, this code does not make much sense. It looks like it wants to check whether printf
failed or suceeded in writing the text. But according to the specification, printf
will return the number of bytes written if success or a negative value if error. So it will return 0 only if it writes 0 chars, and that will not happen with a \n
at the end, and i
will always end up being 0
, either with or without error.
This is not standard C, but a GCC statement expression extension, which allows putting blocks in expressions and returns the value of the last statement in the block.
Because the block here has only one statement which is itself an expression, this is equivalent to:
This sets
i
to 1 ifprintf
returned 0 (i.e. it succeeded but didn't print any characters), or 0 otherwise. Since thisprintf
will always print at least two characters when it succeeds,i
will always be 0.