Preprocessor not doing calculation when const changes

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
steve42lawson
Posts: 183
Joined: 06 Mar 2008 17:35
Location: St. George, UT
Contact:

Preprocessor not doing calculation when const changes

#1 Post by steve42lawson » 17 Jun 2020 19:51

Having an issue with the pre-processor -- probably because I'm not doing things the MikroC way :lol:

The following code consists of a config.h file, and a main.c file, both in the same project directory. When I change the define value, in config.h, and then do a build, or even a rebuild all, and then check the value in the SOME_CALC_USING_CONFIG constant [in the main.c file], the value doesn't change -- suggesting the pre-processor didn't detect the change I made in the SOME_DEFED_VAL value, and thus didn't recalculate the constant - even though, if I Ctrl+Space on the SOME_DEFED_VAL in the main.c file, it shows the new value!

BUT

If I make any sort of change in in the main.c file, the SOME_CALC_USING_CONFIG value updates to the proper value [i.e. the calculation is performed].

BTW: In this case I have the MCU arbitrarily set to P16F15344
BTW2: When I say "main.c" I'm speaking euphemistically -- I really referring to my "BugReport_preProcNoCalc.c" file -- I suppose I shoodda just called it "main.c"!

config.h

Code: Select all

#define SOME_DEFED_VAL   500
main.c

Code: Select all

#include "config.h"

const unsigned int SOME_CALC_USING_CONFIG = (unsigned int)(3 * SOME_DEFED_VAL);

void main() 
{
    while(1)
    {
        ;  // Twiddle digits
    }
}
The following screenshot sequence tells the story [I had more screenshots, but I could only attach 3, so story not as good as it could have been ;) ]:
Following the first compile, the SOME_CALC_USING_CONFIG  value is correct.
Following the first compile, the SOME_CALC_USING_CONFIG value is correct.
SShot03.png (117.19 KiB) Viewed 1028 times
After changing the SOME_DEFED_VAL  in config.h, from 500, to 400, I did another compile.  This screen shot shows that the SOME_DEFED_VAL  correctly registers as 400 in the main.c file.
After changing the SOME_DEFED_VAL in config.h, from 500, to 400, I did another compile. This screen shot shows that the SOME_DEFED_VAL correctly registers as 400 in the main.c file.
SShot06.png (116.73 KiB) Viewed 1028 times
BUT, the SOME_CALC_USING_CONFIG value hasn't changed.  It should be 1200.
BUT, the SOME_CALC_USING_CONFIG value hasn't changed. It should be 1200.
SShot07.png (49.85 KiB) Viewed 1028 times
If I could add just one more screen shot, I could show you the result of adding a space in the main.com file, then recompiling: The SOME_CALC_USING_CONFIG becomes 1200, which is the correct value.

The Compiler/Linker messages show nothing about compiling the config.h file -- sorry that got covered up.
Humility is the lack of the desire to impress, not the lack of being impressive.

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Preprocessor not doing calculation when const changes

#2 Post by Sparky1039 » 18 Jun 2020 06:52

Be careful with the definition of constant. A constant is defined at compile time not run time. By this declaration it is not a "variable" anymore and the code is not allowed to modify it during execution, even when initializing global variables. A better way to deal with this is to generate an equation in the header like...

Code: Select all

#define SOME_DEFED_VAL   500
#define SOME_CALC_USING_CONFIG (SOME_DEFED_VAL * 3)
Or just make this a plain variable. Since you declared it before any code, this makes it global in scope and its computed contents will be preserved throughout the program.

Code: Select all

unsigned int SOME_CALC_USING_CONFIG = 3 * SOME_DEFED_VAL;
Lastly, there is no need to cast the result since the computed value will fit within the declared variable size.

steve42lawson
Posts: 183
Joined: 06 Mar 2008 17:35
Location: St. George, UT
Contact:

Re: Preprocessor not doing calculation when const changes

#3 Post by steve42lawson » 18 Jun 2020 22:20

Sparky1039 wrote:
18 Jun 2020 06:52
Be careful with the definition of constant. A constant is defined at compile time not run time. By this declaration it is not a "variable" anymore and the code is not allowed to modify it during execution, even when initializing global variables. A better way to deal with this is to generate an equation in the header like...

Code: Select all

#define SOME_DEFED_VAL   500
#define SOME_CALC_USING_CONFIG (SOME_DEFED_VAL * 3)
You're so right! Thank you.
Sparky1039 wrote:
18 Jun 2020 06:52
Or just make this a plain variable. Since you declared it before any code, this makes it global in scope and its computed contents will be preserved throughout the program.

Code: Select all

unsigned int SOME_CALC_USING_CONFIG = 3 * SOME_DEFED_VAL;
I actually tried that, but the same thing happened. BUT, the real solution is already stated!
Sparky1039 wrote:
18 Jun 2020 06:52
Lastly, there is no need to cast the result since the computed value will fit within the declared variable size.
I suspected, but decided to leave it in case I ever need to port this code -- I've worked with temperamental [poorly conceived?] compilers that have left me superstitious :wink:
But, am I potentially masking a compiler warning, should the computation exceed the declared variable size -- baring in mind that the SOME_DEFED_VAL could be changed in the future?
Humility is the lack of the desire to impress, not the lack of being impressive.

Post Reply

Return to “mikroC PRO for PIC General”