Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessor error when defining =

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?

like image 366
zubergu Avatar asked Aug 23 '13 15:08

zubergu


People also ask

What is preprocessor error?

A preprocessor error directive causes the preprocessor to generate an error message and causes the compilation to fail.

How do you define a preprocessor?

In computer science, a preprocessor (or precompiler) is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers.

What does ## mean in C preprocessor?

## is Token Pasting Operator. The double-number-sign or "token-pasting" operator (##), which is sometimes called the "merging" operator, is used in both object-like and function-like macros.

What is an example of a preprocessor?

Examples of some preprocessor directives are: #include, #define, #ifndef etc. Remember that the # symbol only provides a path to the preprocessor, and a command such as include is processed by the preprocessor program. For example, #include will include extra code in your program.


1 Answers

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

like image 183