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????

  • "What is happing????"

    1) You forgot to read the instructions on how to post source code. So the layout got totally garbled. How about being a bit more careful with the details. It really helps, if trying to write source code.

    2) You did not spend too much time reading through all the good documentation about the meaning of #define - the glorified string-replace feature of the C language.

    TempW1 = Num1;
    TempW2 = TempW1 - Num1;
    

    Attempt 1:

    #define Num1 22
    
    TempW1 = 22;
    TempW2 = TempW1 - 22;
    

    So TempW2 ended up with 22-22 => 0.

    Attempt 2:

    #define Num1 21+1
    
    TempW1 = 21+1;
    TempW2 = TempW1 - 21+1;
    


    So TempW2 ended up with 22 - 21 + 1 => 2.

    This is covered a huge number of times in a huge number of publications about #define.

    Have you ever seen examples of use of #define together with parentheses?

    What you should have done:

    #define Num1 (21+1)
    
    TempW1 = (21+1);
    TempW2 = TempW1 - (21+1);
    

    Now your TempW2 gets the value 22 - (21+1) => 0.

    Remember what I mentioned under item 1 above? It's all in the details. Start caring about them...

  • ok ... my mistake ... sorry ... I learned this time, I will be more careful ... Thanks for the quick response ...

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