13

I was trying some awkward preprocessing and came up with something like this:

#include <stdio.h>

#define SIX =6

int main(void)
{
  int x=6;
  int y=2;

  if(x=SIX)
    printf("X == 6\n");
  if(y=SIX)
    printf("Y==6\n");

  return 0;
}

gcc gives me the errors:

test.c: In function ‘main’:
test.c:10:8: error: expected expression before ‘=’ token
test.c:12:8: error: expected expression before ‘=’ token

Why is that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
zubergu
  • 3,646
  • 3
  • 25
  • 38
  • 2
    Possible duplicate of [What is the worst real-world macros/pre-processor abuse you've ever come across?](http://stackoverflow.com/questions/652788/what-is-the-worst-real-world-macros-pre-processor-abuse-youve-ever-come-across) Just kidding, good question. – 1'' Aug 23 '13 at 15:59
  • 1
    Running this through the preprocessor, the tests turn into `if(x= =6)`. I'm not sure why the space is inserted... presumably someone who knows one of the C specifications much better than me will come along... – cdhowie Aug 23 '13 at 16:00
  • 4
    The preprocessor deals in tokens, logically with spaces separating the tokens. When it tokenizes `if(x=SIX)`, it has `if`, `(`, `x`, `=`, and `SIX`. When it macro expands `SIX`, it has extra tokens `=` and `6`. But two adjacent tokens `=` are not the same as one token `==` (and are in fact invalid C syntax) — hence the compilation error. – Jonathan Leffler Aug 23 '13 at 16:01
  • Because the result of macro expansion is never rescanned for possible token pasting. – Jens Aug 23 '13 at 19:22

4 Answers4

15

The == is a single token, it cannot be split in half. You should run gcc -E on your code

From GCC manual pages:

-E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.

Input files that don't require preprocessing are ignored.

For your code gcc -E gives the following output

  if(x= =6)
    printf("X == 6\n");

  if(y= =6)
    printf("Y==6\n");

The second = is what causes the error message expected expression before ‘=’ token

5

The preprocessor doesn't work at the character level, it operates at the token level. So when it performs the substitution, you get something equivalent to:

if (x = = 6)

rather than your desired:

if (x==6)

There are some specific exceptions to this, like the # stringification operator.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Where does the blank between `=` and `6` come from? – alk Aug 23 '13 at 16:02
  • 1
    @alk I'm just using the spaces to show token boundaries. As far as the C grammar is concerned, there's no difference between `=6` and `= 6`. – Barmar Aug 23 '13 at 16:04
4
if(x=SIX) 

is parsed as

if (x= =6).

So you get the error.

Hrishi
  • 7,110
  • 5
  • 27
  • 26
0

What toolchain are you using? If you are using GCC, you can add the -save-temps option and check the test.i intermediate result to troubleshoot your problem.

I suspect you have a space between the x= and the =6.

Balau
  • 495
  • 3
  • 9