Search code examples
cpycparser

C - int not-equal to printf?


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


Solution

  • 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:

    i = !printf("%d\n",r);
    

    This sets i to 1 if printf returned 0 (i.e. it succeeded but didn't print any characters), or 0 otherwise. Since this printf will always print at least two characters when it succeeds, i will always be 0.