This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Compiler Erro Keil uVision V4.22.22.0

I don't understand this error:

/*---------------------------------------------------------------------------- * This compiling OK, with Num1=22 *--------------------------------------------------------------------------*/
#define Num1 22

unsigned int TempW1;
unsigned int TempW2;

int main (void)
{ TempW1 = Num1; TempW2 = TempW1 - Num1; while(1);
}

Resul--> TempW2=0; it's OK

But

/*---------------------------------------------------------------------------- * This compiling ERROR, with Num1=21+1 *--------------------------------------------------------------------------*/
#define Num1 21+1

unsigned int TempW1;
unsigned int TempW2;

int main (void)
{ TempW1 = Num1; TempW2 = TempW1 - Num1; while(1);
}

Resul--> TempW2=2; it's ERROR

What is happing????

Parents Reply Children
  • You'll get into many, many more troubles with #define if you don't spend some time reading up on what it does. And best practices for how to use it.

    Missing parentheses is just one danger. Having a #define that takes a parameter with a side effect and make use of it multiple times is another big oops.

    People normally uses all-caps for #define names, just to make it 100% obvious when reading the source code that there may be a need to have an extra look at the #define to verify that the expansion can be trusted.

    By the way - your thread subject claims a compiler error. Are you really sure that is a good subject? Especially since most people who claims to suffer from a compiler error (goes for just about any compiler) are in reality suffering because of own errors.

  • I was not really fair to the compiler ... wrong twice ... or more ... I was too hasty .. thanks for the learning ...